Please fix this php contact form in select option

after adding select menu , i got error message in mail.php

my html form------------------------

<form name="contactform" class="form" method="post" action="mail.php">
  <input type="text" name="first_name" class="form-control" placeholder="Name">
  <div class="help-block with-errors"></div>
  <input  type="text" name="email" class="form-control" placeholder="Email Address">
  <div class="help-block with-errors"></div>
  <input  type="text" name="telephone" class="form-control" placeholder="Phone Number">
  <div class="help-block with-errors"></div>
   <select name="location" form="location" class="form-control">
  <option value="Select Location">Select Location</option>
  <option value="Kannur">Kannur</option>
  <option value="Calicut">Calicut</option>
</select>
  <div class="help-block with-errors"></div>
  <input  type="text" name="subject" value="Car Body Polishing & Teflon Coating" class="form-control"  placeholder="Subject" readonly>
  <div class="help-block with-errors"></div>
  <textarea  name="comments" class="form-control" placeholder="Message" rows="7"></textarea>
  <div class="help-block with-errors"></div>
  <input type="submit" class="button" value="Submit"> 
</form>

my php file-----------------------

<?php
if(isset($_POST['email'])) {
 
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "[email protected]";
    $email_subject = "Booking :: Malar Agencies - Mail Recieved!";
 
    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.";
        die();
    }
 
 
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['location']) ||
        !isset($_POST['subject']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
 
     
 
    $first_name = $_POST['first_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $location = $_POST['location']; // not required
    $subject = $_POST['subject']; // not required
    $comments = $_POST['comments']; // 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 .= '<div class="alert alert-warning">The Email Address you entered does not appear to be valid.</div>';
  }
 
    $string_exp = "/^[A-Za-z .'-]+$/";
 
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= '<div class="alert alert-warning">The First Name you entered does not appear to be valid.</div>';
  }
 
  if(strlen($comments) < 2) {
    $error_message .= '<div class="alert alert-warning">The Comments you entered do not appear to be valid.</div>';
  }
 
  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 .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Location: ".clean_string($location)."\n";
    $email_message .= "Subject: ".clean_string($subject)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\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);  
?>
 </div>
<!-- include your own success html here -->
 <script>
window.onload=function() {
  setTimeout(function() {
    location.replace("index.html");
  },3000); // wait 3 seconds
}
</script>
<div class="alert alert-success">
  <strong>Thank you for contacting us.</strong> We will be in touch with you very soon.<br/>
  <a href="index.html">please click here if you are not redirected in a few seconds</a>
</div>

 
<?php
 
}
?>

Can you tell us what the error message is? Many times it specifies which line the error is occurring on, which can help with debugging.

Also, a few lines above the end of your mail.php file, you have an @ symbol in front of your mail function. Assuming this is in your actual code, it’s going to thrown an error.

1 Like

normlaly its ok bro . but when i add this his code in form , its getting error `

Select Location Kannur Calicut `

You also have a form='location' attribute inside of your html file. Removing that line allows the php file to complete successfully, though performing a var_dump on the mail function reveals that all it does is return false, and not much else.

There may be more issues within the php function, but for now just removing the form='location' from the location selector seems to work

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.