title: Having an Ajax Save button and save in database
body: <p>I'm currently doing a form whereby customers will have to do the survey form, I'll have a AJAX "Save" button to allow me to save the data into database when the customers did not managed to finish the form itself and then when the customers login again, the form which they did halfway will pop out again and ask them to continue finish the survey form. </p>
  
  <p>Is it possible where AJAX/javascript/jQuery can work with php codes in it (because of the insert query)? </p>
  
  <p>Not very sure with AJAX and all so Thanks for helping!</p>
  
  <p>This is for the "Save" button.</p>
  
  <pre><code>&lt;input type="button" onClick="save();" value="Save"&gt;
  </code></pre>
  
  <p>This is the insert query whereby it will be inserted in database.</p>
  
  <pre><code>    &lt;?php
          include("dbFunctions.php");
  
  
          $idQuery = "SELECT id,question,input FROM db_ExtApp1.scFormLayout WHERE surveyID ='$lastID'";
  $idResult = sqlsrv_query($conn, $idQuery);
  
  while ($row = sqlsrv_fetch_array($idResult)) {
      $fcID = $row['id'];
      $question = $row['question'];
      $surveyTitle = $_SESSION['surveyTitle'];
      $input = $row['input'];
  
      if (isset($_POST['qns' . $fcID])) {
      $answer = implode(",", $_POST['qns' . $fcID]);
      $newAns = str_replace($singleQuote,$changeQuote,$answer);
      } else {
          $newAns = '';
      }
      if (isset($_POST['others'.$fcID])) {
      $others = $_POST['others' . $fcID];
      $newOthers = str_replace($singleQuote,$changeQuote,$others);
      }else {
          $newOthers = 'N.A.';
      }
  
  $connectionInfo['ConnectionPooling']=0; // this creates a new connection on the next line...
  $newconn = sqlsrv_connect($serverName, $connectionInfo); 
  if ($input != 'Normal text line, no input required*') {
      $query = "INSERT INTO db_ExtApp1.scFormResult(surveyID, answer, others, title, question, name)
  VALUES ('$lastID','$newAns','$newOthers', '$surveyTitle','$question', '$name')";
  
      $status = sqlsrv_query($newconn, $query);
  } }
      if ($status === false) {
          die(print_r(sqlsrv_errors(), true));
      } 
  sqlsrv_close($conn);
  </code></pre>