title: JavaScript Submit HTML Form via POST
body: <p>I have the following Javascript code:</p>
  
  <pre><code>function checkIfValid(){
      var form = document.createuserform;
  
      if(form.fName.value == "" || form.lName.value == "" || form.pass.value == "" || !isValidEmail()){
      alert("Please Ensure All Fields Are Filled In Correctly");
      }else{
      //submit form
      form.submit();
      }
  }
  
  function isValidEmail(){
      var x=document.forms["createuserform"]["email"].value
      var atpos=x.indexOf("@");
      var dotpos=x.lastIndexOf(".");
      if (atpos&lt;1 || dotpos&lt;atpos+2 || dotpos+2&gt;=x.length)
      {
      return false;
      }
      return true;
  }
  </code></pre>
  
  <p>This for a create user page. The javascript checks to see if the data entered is valid and then submits the form to a php app which then stores the data in the database...</p>
  
  <p>Here is the HTML:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
  &lt;title&gt;Create User&lt;/title&gt;
  &lt;script src="http://dev.speechlink.co.uk/David/js/createuser.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;h1&gt;Create User&lt;/h1&gt;
  
  &lt;p&gt;Please fill in the details below to create a new user account&lt;/p&gt;
  
  &lt;form name = "createuserform" action = "http://dev.speechlink.co.uk/David/createuser.php" method = "post"&gt;
  &lt;p&gt;First Name:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;input name = "fName" type="text" size="25"&gt;&lt;/p&gt;
  
  &lt;p&gt;Last Name:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;input name = "lName" type="text" size="25"&gt;&lt;/p&gt;
  
  &lt;p&gt;E-mail:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;input name = "email" type="text" size="25"&gt;&lt;/p&gt;
  
  &lt;p&gt;Password:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;input name = "pass" type="text" size="25"&gt;&lt;/p&gt;
  
  &lt;p&gt; Gender:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;input type="radio" name="gender" value="M" checked&gt; Male
  &lt;input type="radio" name="gender" value="F"&gt; Female&lt;/p&gt;
  
  &lt;p&gt;User Type: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;select name="usertype"&gt; 
  &lt;option&gt;Athlete&lt;/option&gt;
  &lt;option&gt;Support Staff&lt;/option&gt;
  &lt;/select&gt;&lt;/p&gt;
  
  &lt;p&gt;&lt;input type ="button" value="Create Account" name = "createuser" onclick="checkIfValid()"&gt;&lt;/p&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The php code is as follows:</p>
  
  <pre><code>&lt;?php
  
  session_start();
  
  $dbh = connect();
  
  //connect to database
  function connect() {
    $dbh = mysql_connect ("localhost", "abc123", "def567") or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db("PDS", $dbh); 
    return $dbh;
  }
  
  //insert into database
  if (isset($_POST['createuser'])){
  
      //prevent SQL injections (this version of php does not support mysqli functions)
      $fn = mysql_real_escape_string($_POST['fName']);
      $ln = mysql_real_escape_string($_POST['lName']);
      $email = mysql_real_escape_string($_POST['email']);
      $gender = mysql_real_escape_string($_POST['gender']);
      $usertype = mysql_real_escape_string($_POST['usertype']);
      if($usertype == 'Athlete'){
        $usertype = 'Athletes';
      }else{
        $usertype = 'SupportStaff';
      }
      $pass = md5($_POST['pass']);
  
      if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
  
          $query= "SELECT * FROM $usertype WHERE email = '" . $email . "'";
          $result = mysql_query($query);
  
          if(mysql_num_rows($result) &lt; 1 || !mysql_num_rows($result)) {
  
              $query= "INSERT INTO $usertype (fName, lName, gender, email, password) VALUES ('$fn', '$ln', '$gender', '$email', '$pass')";
              mysql_query($query);
              mysql_close($dbh);
              echo ("&amp;result=true&amp;");         
          } else {
              mysql_close($dbh);
              echo "&amp;result=userexists&amp;";         
          }
      }
      else {
          mysql_close($dbh);
          echo "&amp;result=false&amp;"; //invalid e-mail address
      }   
  }
  ?&gt;
  </code></pre>
  
  <p>The php works when I use a submit button in the HTML form to POST directly to it. But when I use the javascript, the page is targeted but it is blank...nothing is echo'd...Any ideas?</p>
  
  <p>I have a feeling the form is not catching the 'createuser' name of the HTML button firing the javascript...As my php file expects $_POST['createuser'] to be set...perhaps it isn't...?</p>