title: Javascript not passing variable to PHP with HTML form
body: <p>I have contact form, that uses Javascript and PHP.</p>
  
  <p>My problem is that the javascript does not pass its variables to the PHP script. I have discovered the following:</p>
  
  <p>The variables does get set in the javascript, as it passes the variable check inside the javascript and by using alerts showing the variable.</p>
  
  <p>As it travels to the PHP script, the variables are no longer there. I have tried to comment out the check in the PHP script, then it succesfully send a mail, but the mail was empty apart from the static content from the PHP script.</p>
  
  <p>I have tried various different methods, including passing the variables directly from the http.send() function.</p>
  
  <p>Here is the relevant code (These are all 3 different files on the web server):</p>
  
  <p>HTML:</p>
  
  <pre><code>div class="contact_form"&gt;
              &lt;h4&gt;Get in touch&lt;/h4&gt;
              &lt;form method="post"&gt;
                  &lt;input type="text" name="Name" id="name" value="Name" onfocus="this.value = this.value=='Name'?'':this.value;" onblur="this.value = this.value==''?'Name':this.value;" /&gt;
                  &lt;input type="text" name="Email" id="email" value="Email" onfocus="this.value = this.value=='Email'?'':this.value;" onblur="this.value = this.value==''?'Email':this.value;" /&gt;
                  &lt;input type="text" value="Subject (Hosting, Requests, Appeal, Report, etc.)" id="subject" onfocus="this.value = this.value=='Subject (Hosting, Requests, Appeal, Report, etc.)'?'':this.value;" onblur="this.value = this.value==''?'Subject (Hosting, Requests, Appeal, Report, etc.)':this.value;" /&gt;
                  &lt;textarea name="Message" id="body" onfocus="this.value = this.value=='Message'?'':this.value;" onblur="this.value = this.value==''?'Message':this.value;"&gt;Message&lt;/textarea&gt;
                  &lt;input type="submit" name="submit" id="submit" value="send" class="submit-button" onClick="return check_values();" /&gt;
              &lt;/form&gt;
              &lt;div id="confirmation" style="display:none; position: relative; z-index: 600; font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px; color: #4e4e4e;"&gt;&lt;/div&gt;
          &lt;/div&gt; &lt;!-- end .contact_form --&gt;
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code>var http = createRequestObject();
  var areal = Math.random() + "";
  var real = areal.substring(2,6);
  
  function createRequestObject() {
      var xmlhttp;
      try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
    catch(e) {
      try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
      catch(f) { xmlhttp=null; }
    }
    if(!xmlhttp&amp;&amp;typeof XMLHttpRequest!="undefined") {
      xmlhttp=new XMLHttpRequest();
    }
      return  xmlhttp;
  }
  
  function sendRequest() {
      var rnd = Math.random();
      var name = escape(document.getElementById("name").value);
      var email = escape(document.getElementById("email").value);
      var subject = escape(document.getElementById("subject").value);
      var body = escape(document.getElementById("body").value);
  
      try{
          http.open('POST','pform.php');
          http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
          http.send('name='+name+'&amp;email='+email+'&amp;subject='+subject+'&amp;body='+body+'&amp;rnd='+rnd);
          http.onreadystatechange = handleResponse;
      }
      catch(e){}
      finally{}
  }
  
  function check_values() {
      var valid = '';
  
      var name = document.getElementById("name").value;
      var email = document.getElementById("email").value;
      var subject = document.getElementById("subject").value;
      var body = document.getElementById("body").value;
      if(trim(name) == "" ||
          trim(email) == "" ||
          trim(subject) == "" ||
          trim(body) == "") {
              alert("Please complete all fields");
      } else {
          if(isEmail(email)) {
              document.getElementById("submit").disabled=true;
              document.getElementById("submit").value='Please Wait..';
              sendRequest();
          } else {
              alert("Email appears to be invalid\nPlease check and try again");
              document.getElementById("email").focus();
              document.getElementById("email").select();
          }
      }
  }
  
  function handleResponse() {
      try{
      if((http.readyState == 4)&amp;&amp;(http.status == 200)){
          var response = http.responseText;
        document.getElementById("confirmation").innerHTML = response;
        document.getElementById("confirmation").style.display ="";
          }
    }
      catch(e){}
      finally{}
  }
  
  function isUndefined(a) {
     return typeof a == 'undefined';
  }
  
  function trim(a) {
      return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
  }
  
  function isEmail(a) {
     return (a.indexOf(".") &gt; 0) &amp;&amp; (a.indexOf("@") &gt; 0);
  }
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code>&lt;?php
  error_reporting(0);
  
  $page_title = "Contact Us Form";
  $email_it_to = "email@example.com";
  $error_message = "Please complete the form first";
  $confirmation = "Thank you, your message has been successfully sent.";
  
  if(!isset($rnd) || !isset($name) || !isset($email) || !isset($subject) || !isset($body)) {
      echo $error_message;
      die();
  }
  
      $email_from = $email;
      $email_subject = "Contact Form: ".stripslashes($subject);
      $email_message = "Please find below a message submitted by '".stripslashes($name);
      $email_message .="' on ".date("d/m/Y")." at ".date("H:i")."\n\n";
      $email_message .= stripslashes($body);
  
      $headers = 'From: '.$email_from."\r\n" .
     'Reply-To: '.$email_from."\r\n" .
     'X-Mailer: PHP/' . phpversion();
  
      mail($email_it_to, $email_subject, $email_message, $headers);
  
      echo "&lt;b&gt;$confirmation&lt;/b&gt;";
      die();
  ?&gt;
  </code></pre>