title: Using AJAX to send form data to php script (without jQuery)
body: <p>I am trying to do a basic AJAX implementation to send some form data to a php script and db. I'm just doing this for learning purposes, and have taken it as far as I could. When I hit the "Create Profile" button, nothing is happening. From my code below, does anything obvious jump out at anyone in my syntax/structure?</p>
  
  <p>Note* I've yet to implement the code to retrieve the data using AJAX, will do this later once I get the send working. </p>
  
  <p>EDIT*** I made some slight changes to the sendFunction(), and have seen some success. Values are now being added to my database, but they values are blank, instead of the values in the form data.</p>
  
  <p>Thank you for all help/suggestions ahead of time!</p>
  
  <p>HTML doc:</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Ajax Form&lt;/title&gt;
  
      &lt;script language="javascript" type="text/javascript"&gt;
      function sendFunction() {                           // Create a function to handle the Ajax
          var xmlhttpCreate;                              // Variable to hold the xmlhttpRequest object
          if (window.XMLHttPRequest) {                     // Checks for browser compatibilities 
              xmlhttpCreate = new XMLHttpRequest();
          }
          else {
              xmlhttpCreate = new ActiveXObject("Microsoft.XMLHTTP");
          }
  
          xmlhttpCreate.onreadystatechange = function() {                 
              if (xmlhttpCreate.readyState == 4) {        // If server has processed request and is ready to respond
                  document.getElementById("createSuccess").innerHTML = xmlhttpCreate.responseText;  // Display a success message that the data was sent and processed by the php script &amp; database
              }
          }
  
          var fName = document.getElementById('firstName').value;         // Dump user firstName into a variable
          var lName = document.getElementById('lastName').value;         // Dump user lastName into a variable
          var queryString = "?fName=" + fName + "&amp;lName=" + lName;       // A query string that will be sent to the php script, which will then send the values to the db
          xmlhttpCreate.open("GET", "ajax_create.php" + queryString, true);  // Open the request
          xmlhttpCreate.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
          xmlhttpCreate.send();                                            // Send the request
  
      }
      &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
      &lt;h3&gt;Create Profile&lt;/h3&gt;&lt;br&gt;
      &lt;form name="form"&gt;
          First Name: &lt;input type="text" id ="firstName"/&gt;&lt;br&gt;&lt;br&gt;
          Last Name: &lt;input type="text" id="lastName"/&gt;&lt;br&gt;&lt;br&gt;
          &lt;input type="button" onclick="sendFunction()" value="Create Profile"&gt;
      &lt;/form&gt;&lt;br&gt;
      &lt;div id="createSuccess"&gt;&lt;/div&gt;&lt;br&gt;
  
      &lt;h3&gt;Search for Profile&lt;/h3&gt;&lt;br&gt;
      &lt;form name="searchForm"&gt;
          First Name: &lt;input type="text" id="searchFirstName"/&gt;&lt;br&gt;&lt;br&gt;
          &lt;input type="button" onclick="sendFunction()" value="Search for Profile"/&gt;
      &lt;/form&gt;&lt;br&gt;&lt;br&gt;
  
      &lt;div id="resultFN"&gt;&lt;/div&gt;&lt;br&gt;
      &lt;div id="resultLN"&gt;&lt;/div&gt;&lt;br&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And here is my PHP script:</p>
  
  <pre><code>&lt;?php
  
      // Connect to the database
      $con = mysqli_connect('localhost', 'root', 'intell', 'ajax_profile');
  
      // GET variables from xmlhttpCreate
      $fName = $_POST['fName'];
      $lName = $_POST['lName'];
  
      // Escape the user input to help prevent SQL injection
      $fName = mysqli_real_escape_string($fName);
      $lName = mysqli_real_escape_string($lName);
  
      // Build the query
      $query = "INSERT INTO users (firstName, lastName) VALUES ('$fName', '$lName')";
      mysqli_query($con, $query);
      mysqli_close($con);
  
      $success = "Profile added to the database";
      echo $success;
  ?&gt;
  </code></pre>