Help on PHP Email form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chetanh78
Forum Newbie
Posts: 5
Joined: Thu Apr 23, 2009 9:57 pm

Help on PHP Email form

Post by chetanh78 »

I am new to PHP and I seriously need help with this. I created a form.php and formprocessor.php..but when I ran it ..give me parse errors...I checked my spelling..everything..but couldn't figure out...any help will be appreciated

form.php

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Untitled document</title>
</head>
<body>
<form name="contactform" method="post" action="desktops_processor.php"> 
<table width="450">
<tbody>
<tr>
<td valign="top"><label for="first_name">First Name <span style="color: #ff0000;">*</span></label></td>
<td valign="top"><input name="first_name" maxlength="50" size="30" type="text" /></td>
</tr>
<tr>
<td valign="top"><label for="last_name">Last Name <span style="color: #ff0000;">*</span></label></td>
<td valign="top"><input name="last_name" maxlength="50" size="30" type="text" /></td>
</tr>
 
<td valign="top"><label for="address">Address<span style="color: #ff0000;">*</span></label></td>
<td valign="top"><input name="address" maxlength="50" size="40" type="text" /></td>
</tr>
 
<td valign="top"><label for="city">City<span style="color: #ff0000;">*</span></label></td>
<td valign="top"><input name="city" maxlength="50" size="40" type="text" /></td>
</tr>
 
 
<td valign="top"><label for="state">State</span></label></td>
<td valign="top"><select name="state">
<option>UT</option>
</select>
</td>
</tr>
 
<td valign="top"><label for="zipcode">Zip Code:</span></label></td>
<td valign="top"><input name="zipcode" maxlength="5" size="5" type="text" /></td>
</tr>
 
<td valign="top"><label for="telephone">Phone <span style="color: #ff0000;">*</span></label></td>
<td valign="top"><input name="telephone" maxlength="10" size="10" type="text" /><lable>(XXX.XXX.XXXX)</lable></td>
</tr>
<tr>
 
<td valign="top"><label for="comments">Comments <span style="color: #ff0000;">*</span></label></td>
<td valign="top"><textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea></td>
</tr>
<tr>
<tr>
<td colspan="2" style="text-align:center"><input value="Submit" type="submit" /> 
<input value="Clear" type="reset" /></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
 
 


phpprocessor.php

Code: Select all

 
 
<?php
if(isset($_POST['email'])) {
    
 
    $email_to = "email@email.com";
    $email_subject = "Title";
    
    
    function died($error) {
        // your error code can go here
        //echo "There is error with your input!";
        echo "Please fix the following errors:<br /><br />";
        echo $error."<br /><br />";
        //echo "Please go back and fix these errors.<br /><br />";
        die();
    }
    
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
       !isset($_POST['last_name']) ||
       !isset($_POST['address']) ||
       !isset($_POST['city']) ||
       !isset($_POST['state']) ||
       !isset($_POST['zipcode']) ||
       !isset($_POST['telephone']) ||
       !isset($_POST['comments'])) {
          died('We are sorry, but there appears to be a problem with the form your submitted.');        
    }
    
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
      $address = $_POST['address']; //required
      $city = $_POST['city']; //required
      $state = $_POST['state']; //not required
      $zipcode = $_POST['zipcode']; //required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
    
    $error_message = "";
 
    $string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$first_name)) {
    $error_message .= 'First Name.<br />';
  }
  if(!eregi($string_exp,$last_name)) {
    $error_message .= 'Last Name<br />';
  }
 
  $string_exp = "^[1-9][0-9]*\s+([a-zA-Z]+|[a-zA-Z]+\s+[a-zA-Z]+|[a-zA Z]+\s+[a-zA-Z]+\s+[a-zA-Z]+|[a-zA-Z]+\s+[1-9][0-9]*\s+[a-zA-Z]+|[a-zA-Z]+\.\s+[1-9][0-9]*\s+[a-zA-Z]+\.)$";
  if(!eregi($string_exp,$address)) {
    $error_message .= 'Address<br />';
  }
 
 
  $string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$city)) {
    $error_message .= 'City.<br />';
 
 
  $string_exp = "^[A-Z]{2,2}";
  if(!eregi($string_exp,$state)) {
    $error_message .= 'State.<br />';
 
 
  $string_exp = "^\d{5}$";
  if(!eregi($string_exp,$zipcode)) {
    $error_message .= 'ZipCode.<br />';
 
  $string_exp = "^[0-9 .-]+$";
  if(!eregi($string_exp,$telephone)) {
    $error_message .= 'Phone Number<br />';
  }
 
  if(strlen($comments) < 2) {
    $error_message .= 'Comments<br />';
  }
  
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";
    
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
    
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Adress: ".clean_string($address)."\n";
    $email_message .= "City: ".clean_string($city)."\n";
    $email_message .= "State: ".clean_string($state)."\n";
    $email_message .= "ZipCode: ".clean_string($zipcode)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";
    
@mail($email_to, $email_subject, $email_message);  
?>
Thank you!
 
<?
}
?>
 
 
 
 
MasterBeta
Forum Commoner
Posts: 38
Joined: Thu Apr 02, 2009 4:35 am
Location: Lincoln, NE

Re: Help on PHP Email form

Post by MasterBeta »

You were not closing your if statements from lines 58 - 69.
I had problems validating the address too. I'm horrible at regex but I'm not sure that validating a street address is even possible with so many different types out there. I just made it so if the user puts 5-60 chars in, it qualifies as a valid address. You may want to ask for someone else's advice, because I suck at regex. Also in your html form there is no text box for email address. I did not add an email validation to your script but you should. I did add lines 2, 90 - 92. $yourForm which should point to your html form. This was added in case the user called phpprocessor.php without submitting the form, in which case will redirect the user to the form page.

Code: Select all

<?php
$yourForm = "form.php"; // added to redirect users to your form page if $_POST data not present
$email_to = "email@email.com";
$email_subject = "Title";
 
if(isset($_POST['email'])) {
    function died($error) {
        // your error code can go here
        echo "Please fix the following errors:<br /><br />";
        echo $error."<br /><br />";
        die();
    }
    
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['address']) ||
    !isset($_POST['city']) ||
    !isset($_POST['state']) ||
    !isset($_POST['zipcode']) ||
    !isset($_POST['telephone']) ||
    !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form your submitted.');       
    }
       
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $address = $_POST['address']; //required
    $city = $_POST['city']; //required
    $state = $_POST['state']; //not required
    $zipcode = $_POST['zipcode']; //required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
    
    $error_message = "";
    $string_exp = "^[a-z .'-]+$";
    if(!eregi($string_exp,$first_name)) {
        $error_message .= 'First Name.<br />';
    }
    if(!eregi($string_exp,$last_name)) {
        $error_message .= 'Last Name<br />';
    }
    $string_exp = ".{5,60}$";
    if(!eregi($string_exp,$address)) {
        $error_message .= 'Address.<br />';
    }
    $string_exp = "^[a-z .'-]+$";
    if(!eregi($string_exp,$city)) {
        $error_message .= 'City.<br />';
    }
    $string_exp = "^[A-Z]{2,2}";
    if(!eregi($string_exp,$state)) {
        $error_message .= 'State.<br />';
    }
    $string_exp = "[0-9]{5}$";
    if(!eregi($string_exp,$zipcode)) {
        $error_message .= 'ZipCode.<br />';
    }
    $string_exp = "^[0-9 .-]+$";
    if(!eregi($string_exp,$telephone)) {
        $error_message .= 'Phone Number<br />';
    }
    if(strlen($comments) < 2) {
        $error_message .= 'Comments<br />';
    }
    if(strlen($error_message) > 0) {
        died($error_message);
    }
    
    $email_message = "Form details below.\n\n";
    
    function clean_string($string) {
        $bad = array("content-type","bcc:","to:","cc:","href");
        return str_replace($bad,"",$string);
    }
       
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Adress: ".clean_string($address)."\n";
    $email_message .= "City: ".clean_string($city)."\n";
    $email_message .= "State: ".clean_string($state)."\n";
    $email_message .= "ZipCode: ".clean_string($zipcode)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";
    
    @mail($email_to, $email_subject, $email_message);  
?>
Thank you!
<?php
}else{
    header("Location:$yourForm");
}
?>
chetanh78
Forum Newbie
Posts: 5
Joined: Thu Apr 23, 2009 9:57 pm

Re: Help on PHP Email form

Post by chetanh78 »

I appreciated for you help. Yes, I forgot to get the email off ..b/c I don't want to get email from users..when you talk I miss closing tag for my if statement from line 59 - 68 (how many closing tag i miss, I found only one)...well, I tested but it gave me the same error...do u mind if you take a look again. thanks! don't worry about regex....

Code: Select all

 
<?php
    $yourForm = "file.php";
 
    $email_to = "@gmail.com";
    $email_subject = "Title";
    
    
    function died($error) {
        // your error code can go here
        //echo "There is error with your input!";
        echo "Please fix the following errors:<br /><br />";
        echo $error."<br /><br />";
        //echo "Please go back and fix these errors.<br /><br />";
        die();
    }
    
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
       !isset($_POST['last_name']) ||
       !isset($_POST['address']) ||
       !isset($_POST['city']) ||
       !isset($_POST['state']) ||
       !isset($_POST['zipcode']) ||
       !isset($_POST['telephone']) ||
       !isset($_POST['comments'])) {
          died('We are sorry, but there appears to be a problem with        the form your submitted.');     
    }
    
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
      $address = $_POST['address']; //required
      $city = $_POST['city']; //required
      $state = $_POST['state']; //not required
      $zipcode = $_POST['zipcode']; //required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
    
    $error_message = "";
 
    $string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$first_name)) {
    $error_message .= 'First Name.<br />';
  }
  if(!eregi($string_exp,$last_name)) {
    $error_message .= 'Last Name<br />';
  }
 
  $string_exp = "^[1-9][0-9]*\s+([a-zA-Z]+|[a-zA-Z]+\s+[a-zA-Z]+|[a-zA Z]+\s+[a-zA-Z]+\s+[a-zA-Z]+|[a-zA-Z]+\s+[1-9][0-9]*\s+[a-zA-Z]+|[a-zA-Z]+\.\s+[1-9][0-9]*\s+[a-zA-Z]+\.)$";
  if(!eregi($string_exp,$address)) {
    $error_message .= 'Address<br />';
  }
 
 
  $string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$city)) {
    $error_message .= 'City.<br />';
 
  $string_exp = "^[A-Z]{2,2}";
  if(!eregi($string_exp,$state)) {
    $error_message .= 'State.<br />';
 
 
  $string_exp = "^\d{5}$";
  if(!eregi($string_exp,$zipcode)) {
    $error_message .= 'ZipCode.<br />';
  }
  $string_exp = "^[0-9 .-]+$";
  if(!eregi($string_exp,$telephone)) {
    $error_message .= 'Phone Number<br />';
  }
 
  if(strlen($comments) < 2) {
    $error_message .= 'Comments<br />';
  }
  
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";
    
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
    
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Adress: ".clean_string($address)."\n";
    $email_message .= "City: ".clean_string($city)."\n";
    $email_message .= "State: ".clean_string($state)."\n";
    $email_message .= "ZipCode: ".clean_string($zipcode)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";
    
@mail($email_to, $email_subject, $email_message);  
?>
Thank you!
 
<?php
}else{
 header("Location:$yourForm");
}
?>
 
 
MasterBeta
Forum Commoner
Posts: 38
Joined: Thu Apr 02, 2009 4:35 am
Location: Lincoln, NE

Re: Help on PHP Email form

Post by MasterBeta »

If you replace your file with the one I posted you won't get any errors.
But you removed the line

Code: Select all

if(isset($_POST['email'])) {
which is required.

Replace

Code: Select all

 $string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$city)) {
    $error_message .= 'City.<br />';
 
With

Code: Select all

 $string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$city)) {
    $error_message .= 'City.<br />';
  }
 
and Replace

Code: Select all

 $string_exp = "^[A-Z]{2,2}";
  if(!eregi($string_exp,$state)) {
    $error_message .= 'State.<br />';
 
With

Code: Select all

 $string_exp = "^[A-Z]{2,2}";
  if(!eregi($string_exp,$state)) {
    $error_message .= 'State.<br />';
  }
 
Post Reply