Little coding help for a form

Discussion in 'PHP' started by Voasi, Jul 11, 2006.

  1. #1
    I need this simple form script to require certain fields within the form to be filled out when sending. I'm not sure what needs to be edited. Here's the script I'm using:

    $subject = 'Price/Quote Request';                // Subject of email sent to you.
    $emailadd = 'email@yahoo.com';        // Your email address. This is where the form information will be sent.
    $url = 'http://www.google.com/thanks.html';               // Where to redirect after form is processed.
    $req = '1';                                  // Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
    
    // --------------------------Do not edit below this line--------------------------
    $text = "Results from form:\n\n";       
    $space = '  ';
    $line = '
    ';
    foreach ($_POST as $key => $value)
    {
    	if ($req == '1')
    	{
    		if ($value == '')
    		{echo "$key is empty";die;}
    	}
    	$j = strlen($key);
    		if ($j >= 20)
    		{echo "Name of form element $key cannot be longer than 120 characters";die;}
    	$j = 20 - $j;
    		for ($i = 1; $i <= $j; $i++)
    		{$space .= ' ';}
    	$value = str_replace('\n', "$line", $value);
    	$conc = "{$key}:$space{$value}$line";
    	$text .= $conc;
    	$space = '  ';
    }
    mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
    ?>
    PHP:

     
    Voasi, Jul 11, 2006 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    So... What doesn't work? What's the error? Where does it go wrong?
     
    T0PS3O, Jul 11, 2006 IP
  3. Voasi

    Voasi Active Member

    Messages:
    1,054
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    88
    #3
    It works great. But on line 4 from above, you can only choose to have all the fields required or none. I need to specify what fields are required.
     
    Voasi, Jul 11, 2006 IP
  4. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Post the form, without it we won't be able to tell.

    Seems like an ugly script to me, it just dies if something required isn't filled out, without a warning even. I'd get another script.
     
    T0PS3O, Jul 11, 2006 IP
  5. Voasi

    Voasi Active Member

    Messages:
    1,054
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    88
    #5
    Here's the form. simplescript.php is the script from above. Nothing special...pretty basic -

                      <form name="form1" method="post" action="simplescript.php">
                        <table width="100%" border="0" cellspacing="0" cellpadding="3">
                          <tr> 
                            <td colspan="2"><div align="center"><img src="images/locator_image.gif" width="378" height="60"></div></td>
                          </tr>
                          <tr> 
                            <td width="43%"><div align="right" class="style2">First 
                                and Last Name<font color="#FF0000">*</font>:</div></td>
                            <td width="57%"><div align="left"> 
                                <input type="text" name="Name" size="30">
    
                              </div></td>
                          </tr>
                          <tr> 
                            <td><div align="right" class="style2">Home/Cell/Work Phone<font color="#FF0000">*</font>:</div></td>
                            <td><div align="left"> 
                                <input type="text" name="Phone" size="30">
                              </div></td>
                          </tr>
    
                          <tr> 
                            <td><div align="right" class="style2">E-mail Address<font color="#FF0000">*</font>:</div></td>
                            <td><div align="left"> 
                                <input type="text" name="Email" size="30">
                              </div></td>
                          </tr>
                          <tr> 
                            <td><div align="right" class="style2">Approximate Moving 
                                Date<font color="#FF0000">*</font>:</div></td>
    
                            <td><div align="left"> 
                                <input type="text" name="Moving_Date" size="30">
                              </div></td>
                          </tr>
                          <tr> 
                            <td><div align="right" class="style2">Price Range<font color="#FF0000">*</font>:</div></td>
                            <td><div align="left">
                                <input type="text" name="Price_Range" size="30">
    
                              </div></td>
                          </tr>
                          <tr> 
                            <td colspan="2"><div align="center"><font color="#FF0000" size="1">* Required</font></div></td>
                          </tr>
                          <tr> 
                            <td colspan="2"><div align="center"> 
                                <input type="submit" name="Submit" value="Submit for Apartments in Houston">
                              </div></td>
                          </tr>
    
                        </table>
                      </form>
    PHP:
     
    Voasi, Jul 11, 2006 IP
  6. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Change:
    
    $req = '1';
    
    PHP:
    To:

    $req_array = array('Name' => 1, 'Phone' => 1, 'Email' => 1, 'Moving_Date' => 1, 'Price_Range' => 1); //Change 1 to 0 if not required
    PHP:
    Then change line 13 and down:

    
    if ($req == '1')
        {
            if ($value == '')
            {echo "$key is empty";die;}
        }
    PHP:
    To:

    
    if ($req_array($value) == '1')
        {
            if ($value == '')
            {echo "$key is empty";die;}
        }
    PHP:
    OTTOMH that should do it.
     
    T0PS3O, Jul 11, 2006 IP
  7. Voasi

    Voasi Active Member

    Messages:
    1,054
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    88
    #7
    ah, sweet! Thanks! I'll give that a try.
     
    Voasi, Jul 11, 2006 IP