title: Getting AJAX to talk to PHP using post
body: <p>I am trying to get my Ajax code to talk to my PHP file before returning back to the same page.</p>
  
  <p>I want my form to be filled in and as soon as send is hit, it talks to my AJAX code, which therefore talks to the PHP file before returning with a success or failure message to end user. </p>
  
  <p>The Ajax code is:</p>
  
  <pre><code>$(document).ready(function(){
      $(function() {  
          $(".submit").click(function() {  
              // validate and process form here
              var fname = $("#fname").val() ;
              var lname = $("#lname").val();
              var aemail = $("#aemail").val() ;
              var year = $("#year").val();
              var club = $("#club").val() ;
              var position = $("#position").val();
              var dataString = 'name='+ name + '&amp;fname=' + fname + '&amp;lname=' + lname + '&amp;aemail='      + aemail + '&amp;year=' + year + '&amp;club=' + club + '&amp;position=' + position;  
  
  
              $.ajax({  
                  type: "POST",  
                  url: "acad_process.php",  
                  data: dataString,  
                  success: function(){      
                      $('#contact_form').html("&lt;div id='message'&gt;&lt;/div&gt;");  
                      $('#message').html("&lt;h2&gt;Contact Form Submitted!&lt;/h2&gt;")
                      .append("&lt;p&gt;Thank you. We will be in touch soon.&lt;/p&gt;")  
                  };
                  return false;
  
                  error: function(){
                      $('#contact_form').html("&lt;div id='message'&gt;&lt;/div&gt;");  
                      $('#message').html("&lt;h2&gt;Contact Form Failed!&lt;/h2&gt;")  
                      .append("&lt;p&gt;Sorry, your form returned an error! Please try again.&lt;/p&gt;")  
                      .hide()  
                      .fadeIn(1500, function() {  
                      });
                  };
                  return false;      
              });  
          });
      });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>Any help would be much appreciated. As I am new to AJAX. Thanks</p>
  
  <p>The issue appears to be PHP because on further investigation I get a error 500 omn the PHP page, and nothing gets returned to my page. The PHP code is:</p>
  
  <pre><code>    &lt;?php
  
     $email_to = "example@domain.co.uk";
  
     if (!empty($_POST))
     {
     $fname = $_POST["fname"];
     $lname = $_POST["lname"];
     $email_from = $_POST["aemail"];
     $year = $_POST ["year"];
     $club = $_POST ["club"];
     $position = $_POST ["position"];
  
     $email_subject = "Enquiry";
  
      $headers  = 'MIME-Version: 1.0' . "\r\n";
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  $headers = "From: $email_from . \n";
  $headers = "Reply-To: $email_from . \n";
  
  $message = "&lt;p&gt; You have received a message!&lt;/p&gt;";
      $message .= "&lt;p&gt;&lt;strong&gt;First Name:&lt;/strong&gt;" . $fname . "&lt;/p&gt;";
      $message .= "&lt;p&gt;&lt;strong&gt;Surname:&lt;/strong&gt;" . $lname . "&lt;/p&gt;";
      $message .= "&lt;p&gt;&lt;strong&gt;School Year:&lt;/strong&gt; " . $year . "&lt;/p&gt;";
      $message .= "&lt;p&gt;&lt;strong&gt;E-Mail:&lt;/strong&gt;" . $aemail ."&lt;/p&gt;";
      $message .= "&lt;p&gt;&lt;strong&gt;Club:&lt;/strong&gt; " . $club . "&lt;/p&gt;";
      $message .= "&lt;p&gt;&lt;strong&gt;Position:&lt;/strong&gt;" . $position . "&lt;/p&gt;";
  
      ini_set("sendmail_from",$email_from);
      mail($email_to, $email_subject, $message, $headers, '-f'.$email_from);
      }
  
      ?&gt;
  </code></pre>