title: Update form fields with AJAX and PHP
body: <p>I have a form that can be edited by a user. I want the form to update without page reload using Ajax. The new values will replace the old values. For the input fields im reading in the values from a php SQL query. So the user will be able to see there details at first sight. The problem is when this code runs im getting no error and no result. I have tried console.log.</p>
  
  <p>I'm getting the users value from the while loop.</p>
  
  <pre><code>if(isset($_GET['edit_user'])){
      $the_user_id =  $_GET['edit_user'];
      $query = "SELECT * FROM users WHERE user_id = $the_user_id ";
      $select_users_query = mysqli_query($connection,$query);  
  
      while($row = mysqli_fetch_assoc($select_users_query)) {
          $user_id        = $row['user_id'];
          $user_firstname = $row['first_name'];
          $user_lastname  = $row['last_name'];
          $user_contact = $row['mobile'];
  
  
        $_SESSION["id"] = $user_id;
      }  
  }
  </code></pre>
  
  <p>Ajax code:</p>
  
  <pre><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  $(document).ready(function(){
      $('#update').click(function(){
          var user_id =  $("#user_id").val();
          var firstname = $("#firstname").val();
          var lastname = $("#lastname").val();
          var contact = $("#mob").val();
          var dataString = 'firstname='+firstname + 'lastname='+lastname + 'contact='+contact+'user_id='+user_id;
  
          if(firstname=='' || lastname=='' || contact=='') {
              alert("Please fill all fields");
          } else {
              $.ajax({
                  type: "POST",
                  url: "update.php",
                  data: dataString,
                  cache: false,
                  success: function(html){
                      alert(html);
                  }
              });
          }
          return false;
      });
  }); 
  &lt;/script&gt;
  </code></pre>
  
  <p>The form I want to be able to edit without page reloading.</p>
  
  <pre><code>&lt;form method="post" name="form"&gt;
      &lt;div style="position:relative; left:120px;"&gt;
          &lt;p&gt;Title: &lt;select&gt;&lt;option value="Mr"&gt;Mr&lt;/option&gt; &lt;option value="Mrs"&gt;Mrs&lt;/option&gt;&lt;option value="Miss"&gt;Miss&lt;/option&gt;&lt;option value="Ms"&gt;Ms&lt;/option&gt;&lt;option value="Dr"&gt;Dr&lt;/option&gt;&lt;/select&gt;&lt;/p&gt;
          &lt;p&gt;First name *: &lt;input type="text" id="firstname" name="firstname" value="&lt;?php echo $user_firstname; ?&gt;" style="width:50%;"&gt;&lt;/p&gt;
          &lt;p&gt;Last name *: &lt;input type="text" id="lastname" name="lastname" value="&lt;?php echo $user_lastname; ?&gt;" style="width:50%;"&gt;&lt;/p&gt;
          &lt;p&gt;Contact telephone number *: &lt;input type="text" id="mob" name="contact" value="&lt;?php echo $user_contact; ?&gt;" style="width:50%;"&gt;&lt;/p&gt;
                      &lt;p&gt;&lt;input type="hidden" id="user_id" name="user_id" value="&lt;?php echo $_SESSION['user_id']; ?&gt;" style="width:50%;"&gt;&lt;/p&gt;
          &lt;button class="btn btn-primary btn-lg" id="update" name="update" role="navigation" type="submit" style="border-radius:0px;"&gt;Save &amp; continue&lt;/button&gt;
      &lt;/div&gt;
  &lt;/form&gt; 
  </code></pre>
  
  <p>The update.php </p>
  
  <pre><code>         &lt;?php require_once("includes/db.php"); ?&gt;
  
               if(isset($_POST['update'])){
  
                $firstname = mysqli_real_escape_string($_POST['firstname']);
               $lastname =  mysqli_real_escape_string($_POST['lastname']);
               $contact =  mysqli_real_escape_string($_POST['contact']);
               $user_id =  mysqli_real_escape_string($_POST['user_id']);
  
                $query = "UPDATE users
            SET first_name   ='". $_POST['firstname'] . "',
                last_name ='". $_POST['lastname'] . "',
                mob    ='". $_POST['contact'] . "'
  
           WHERE
               user_id = '". $_POST['user_id'] . "'";
  
  
  
          $edit_user_query = mysqli_query($connection,$query);
  
  
  
  }                     
  </code></pre>