Need help with a bug in my contact form

Hi,

I updated a small form to add a little spam security. I have tried to implement a simple math question but I am getting loads of php errors, I’ve made mistakes somewhere but cant see where.

I now get bugs when I open the page in localhost. Any help would be much appreciated.

		<?php
            $name = $_POST['name'];
            $email = $_POST['email'];
            $company = $_POST['company'];
	   $phone = $_POST['phone'];
	   $message = $_POST['message'];
			
            $to = 'me@mydomain.com'; 
            $subject = 'User Mail';
            $human = $_POST['human'];
                    
            $body = "From: $name\
 
					 E-Mail: $email\
 
					 Company: $company\
 
					 Phone: $phone\
 
					 Message:\
 $message";
            
            $headers = "From: Website Visitor <someone_from@yourwebsite.com>";
        
                        
        if ($_POST['submit']) {
            if ($name != '' && $email != '' && $company != '' && $phone != '' && $message != '') {
                if ($human == '10') {				 
                    if (mail ($to, $subject, $body, $headers)) { 
                    echo '<h2>Your message has been sent! :) We will get back to you very soon.</h2>';
                } else { 
                    echo '<h2>Something went wrong. Please try again.</h2>'; 
                } 
            } else if ($_POST['submit'] && $human != '10') {
                echo '<h2>You answered the anti-spam question incorrectly. Please try again.</h2>';
            }
            } else {
                echo '<h2>You need to fill in all the fields.  </h2>';
            }
        }
        
        ?>
   

   <form method="post" action="enquiry_form.php">
                  
              <div class="form_left">
                  <p>Name:<br>
                  <input type="text" name="name" size="24" autofocus="true" placeholder="Type Here" required="true" /></p>
              </div>
         
         
               <div class="form_right">
                  <p>Email:<br>
                  <input type="email" name="email" size="24" placeholder="Type Here" required="true" /></p>
              </div>
              
          
             
              <div class="form_left">
                  <p>Company Name:<br />
                  <input type="text" name="company" size="24" placeholder="Type Here" required="true" /></p>
             </div>
         
         
               <div class="form_right">
                   <p>Phone Number:<br />
                  <input type="text" name="phone" size="24" placeholder="Type Here" required="true" /></p>
              </div>
            
        
             <div class="form_left">
                    <p>Your Message:<br />
                     <textarea rows="6" name="message" cols="55" placeholder="Type Here" required="true"></textarea></p>
            
             </div>
             
             
              <div class="form_left">
                    <p>What is 7 + 3 (Anti Spam):<br />
                     <input type="text" name="human" size="24" placeholder="Type Here" required="true" /></p>
             </div>
             
             
            
              <div class="send_callback">
              <p class="submit"><input type="submit" value="Send" name="submit" /></p>
             </div>


</form>


I was testing this in local host! Hmm.

I put it live and it seems to work no problem at all.

Do these forms not perform in localhost?

Mails are sent via mail server. If you local host did not set up one most likely it will not work.

I made some changes to your form where the form does not require re-input all the entries:

<?php
   $name = $_POST['name'];
   $email = $_POST['email'];
   $company = $_POST['company'];
   $phone = $_POST['phone'];
   $message = $_POST['message'];
			
   $to = 'me@mydomain.com';
   $subject = 'User Mail';
   $human = $_POST['human'];

   $body = "From: $name\
".
	     "E-Mail: $email\
".
	     "Company: $company\
".
	     "Phone: $phone\
".
	     "Message:\
 $message";

   $headers = "From: Website Visitor <someone_from@yourwebsite.com>";


   if ($_POST['submit'])
   {
	   if ($name != '' && $email != '' && $company != '' && $phone != '' && $message != '') {
		   if ($human == '10') {				
			   if (mail ($to, $subject, $body, $headers)) {
				   echo '<h2>Your message has been sent! :) We will get back to you very soon.</h2>';
				   exit;
			   } else {
				   echo '<h2>Something went wrong. Please try again.</h2>';
			   }
		   } else if ($_POST['submit'] && $human != '10') {
			   echo '<h2>You answered the anti-spam question incorrectly. Please try again.</h2>';
		   }
	   } else {
		   echo '<h2>You need to fill in all the fields.  </h2>';
	   }
   }


echo '
<form method="post" action="enquiry_form.php">
   <div class="form_left">
      <p>Name:<br>
	 <input type="text" name="name" size="24" autofocus="true" placeholder="Type Here" required="true" value="'.$name.'"/></p>
   </div>
   <div class="form_right">
      <p>Email:<br>
	 <input type="email" name="email" size="24" placeholder="Type Here" required="true" value="'.$email.'" /></p>
   </div>
   <div class="form_left">
      <p>Company Name:<br />
	 <input type="text" name="company" size="24" placeholder="Type Here" required="true" value="'.$company.'" /></p>
   </div>
   <div class="form_right">
      <p>Phone Number:<br />
	 <input type="text" name="phone" size="24" placeholder="Type Here" required="true" value="'.$message.'" /></p>
   </div>
   <div class="form_left">
      <p>Your Message:<br />
	 <textarea rows="6" name="message" cols="55" placeholder="Type Here" required="true">'.$message.'</textarea></p>
   </div>
   <div class="form_left">
      <p>What is 7 + 3 (Anti Spam):<br />
	 <input type="text" name="human" size="24" placeholder="Type Here" required="true" /></p>
   </div>
   <div class="send_callback">
      <p class="submit"><input type="submit" value="Send" name="submit" /></p>
   </div>
</form>';
?>

Thanks for your reply, can you explain what you did please ? Am I right in thinking that the form will remember the details and keep them filled in should the user make a mistake?

I am still very much a php student.

Basically I changed the form to populate with the contents that have already been entered so if you enter an invalid anti-spam code you would not have to re-input everything; otherwise the form would show the placeholder texts again.

We all started as student. If you keep at it you will be a teacher soon enough.