I could use someone's wisdom with this PHP Email Form.

Hello all,

I was wondering if anyone here could help me with the following PHP email form/send-form that I tried to put together for our city’s website. I managed to get the form together, but I am having trouble with the send-form portion which always gives me an error.

My level of PHP is beginner. More or less, I re-use code and often can pin-point the errors – but this time it’s been two days and I feel like I am banging my head against the wall.

This is the form.html portion

 <div id="form_container">
              <h2>Website Feedback</h2>
              <form id="feedback" class="feedback"  method="post" action="send_feedback.php">
                <div class="form_description">
                  <p>Your feedback is greatly appreciated.  We will not be sharing your contact information with anyone.</p>
                </div>
                <ul >
                  <li class="section_break">
                    <h3>Please rate the following categories:</h3>
                  </li>
                  <li>
                    <label class="description"> How often do you visit our site? </label>
                    <select name="visits">
                      <option value="1" >Every day</option>
                      <option value="2" selected="selected">Several times a week</option>
                      <option value="3" >About once a week</option>
                      <option value="4" >Several times a month</option>
                      <option value="5" >About once a month</option>
                      <option value="6" >Less than once a month</option>
                      <option value="7" >This is my first visit</option>
                    </select>
                  </li>
                  <li>
                    <label class="description" >How satisfied are you with your experience with our website? </label>
                    <select name="satisfaction">
                      <option value="1" >Satisfied</option>
                      <option value="2" selected="selected">Neutral</option>
                      <option value="3" >Dissatisfied</option>
                    </select>
                  </li>
                  <li>
                    <label class="description" >Did the menu of items on the home page make sense to you? </label>
                    <span>
                    <input name="nav[]" type="radio" value="1" />
                    <label for="nav[]" class="description" >Yes</label>
                    </span><span style="padding-left: 10px;">
                    <input name="nav[]" type="radio" value="2" />
                    <label for="nav[]" class="description" >No</label>
                    </span>
                  </li>
                  <li>
                    <label class="description" >If no, what would you like to see changed? </label>
                    <textarea name="nav_no" rows="4" cols="35"></textarea>
                  </li>
                  <li>
                    <label class="description" >What is it about this site that you would most like to see improved? </label>
                    <textarea name="improve" rows="4" cols="35"></textarea>
                  </li>
                  <li>
                    <label class="description" >What changes or additional features would you suggest for this website? </label>
                    <textarea name="features" rows="4" cols="35"></textarea>
                  </li>
                  <li class="section_break">
                    <h3>The information below is optional. In cases where clarification may be needed, we may contact you for more details.<br /><br />
                      <b>Contact Information:</b></h3>
                  </li>
                  <li>
                    <label class="description">Name </label>
                    <span>
                    <input name="namef" maxlength="255" size="14" value=""/>
                    <label for="namef" class="description">First</label>
                    </span><span>
                    <input name= "namel" maxlength="255" size="14" value=""/>
                    <label for="namel" class="description">Last</label>
                    </span> </li>
                  <li>
                    <label class="description">Please enter e-mail address: </label>
                    <input name="email" type="text" maxlength="255" value="@"/>
                  </li>
                  <li> 
                    <label class="description" >Phone </label>
                    <span>
                    <input name="area" size="3" maxlength="3" value="" type="text">
                    -
                    <label for="area" class="description">(###)</label>
                    </span> <span>
                    <input name="prefix" size="3" maxlength="3" value="" type="text">
                    -
                    <label for="prefix" class="description">###</label>
                    </span> <span>
                    <input name="line" size="4" maxlength="4" value="" type="text">
                    <label for="line" class="description"><center>####</center></label>
                    </span> </li>
                  <li>
                    <input type="submit" name="submit" value="Submit" />
                  </li>
                </ul>
              </form>
            </div>

Here is the PHP Action / send-feedback.php

<?php
if(isset($_POST['email'])) {
     
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "[email protected]";
    $email_subject = "Website Feedback";
	$full_name ="$namef $namel";
	$phone ="$area - $prefix - $line";
     
     
    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['visits']) ||
		!isset($_POST['satisfaction']) ||
		!isset($_POST['nav']) ||
		!isset($_POST['nav_no']) ||
		!isset($_POST['improve']) ||
		!isset($_POST['features']) ||
		!isset($_POST['full_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['phone'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }
     
    $full_name = $_POST['full_name']; // required
    $email_from = $_POST['email']; // required
    $phone = $_POST['phone']; // not required
    $visits = $_POST['visits']; // required
    $satisfaction = $_POST['satisfaction']; // required
    $nav = $_POST['nav']; // required
    $nav_no = $_POST['nav_no']; // required
    $improve = $_POST['improve']; // required
    $features = $_POST['features']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$namef)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$namel)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<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($namef)."\n";
    $email_message .= "Last Name: ".clean_string($namel)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($phone)."\n";
    $email_message .= "Visits: ".clean_string($visits)."\n";
	$email_message .= "Satisfaction: ".clean_string($satisfaction)."\n";
	$email_message .= "Navigation makes sense: ".clean_string($nav)."\n";
	$email_message .= "If Not: ".clean_string($nav_no)."\n";
	$email_message .= "Improvements: ".clean_string($improve)."\n";
	$email_message .= "Features to add: ".clean_string($features)."\n";
    
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
?>
 
<!-- include your own success html here -->
 
Thank you for contacting us. We will be in touch with you very soon.
 
<?php
}
?>

Any suggestions or feedback regarding the form is greatly appreciated. I welcome thank you in advance for any assistance.

-Son

Try turning error reporting on in the php script;

ini_set('display_errors', 'On'); error_reporting(-1); at the very top. (right after <?php)

Thanks Ray! That did the trick.

Now my next question to solve the problem:

The errors traced to these lines. I want to separate last name and first name, i did so in the form with variables namef & namel. In the POST php file, I set another variable call $full_name = “$namef $namel” – but apparently this isn’t the case. I’m sure it’s something simple like $full_name=$_POST[‘namef’][’$namel’], isn’t it?

In feedback.html

<label class="description">Name </label>
                    <span>
                    <input name="namef" maxlength="255" size="14" value=""/>
                    <label for="namef" class="description">First</label>
                    </span><span>
                    <input name= "namel" maxlength="255" size="14" value=""/>
                    <label for="namel" class="description">Last</label>
                    </span> </li>

In send.feedback.php

   $full_name ="$namef $namel";
   $phone ="$area - $prefix - $line";
$full_name = $_POST['namef']." ".$_POST['namel'];

If you are entering this into mysql then use:

$full_name = mysql_real_escape_string($_POST['namef'])." ".mysql_real_escape_string($_POST['namel']);

Thank you much. My whole send.feedback.php was giving me errors because of the validations for some reason. I decided since this feedback form is temporary – I will just get rid of the validation and it seems to be working fine.

Thank you again for your assistance. Saved me lots of hours.

Sponsor our Newsletter | Privacy Policy | Terms of Service