title: MySQL/PHP: Update MySQL with Ajax
body: <p>I am a beginner and I am trying to learn how to add, delete, retrieve and update a database using PHP and Ajax.</p>
  
  <p>At this time I have accomplished how to retrieve and delete so I am trying to update some values. For retrieving data, I just pass the selected ID I want, so I can retrieve the data. Same goes for delete, I just assign which ID I want to delete. Now to update there are more things going on, its where I cant find the solution.</p>
  
  <p>This is my Form:</p>
  
  <pre><code>&lt;form onsubmit="updateuser()"&gt;
  
     ID Number: &lt;input name="ud_id"&gt;&lt;br&gt;
     First Name: &lt;input type="text" name="ud_first"&gt;&lt;br&gt;
     Last Name: &lt;input type="text" name="ud_last"&gt;&lt;br&gt;
  
  &lt;input type="Submit" value="Update"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>and this is my javascript:</p>
  
  <pre><code>    function updateuser() {
      var str = $('.ud_id').attr('value');
  
     if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
     }
     xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) {
           document.getElementById("txtuser").innerHTML = xmlhttp.responseText;
        }
     }
     xmlhttp.open("GET", "ajaxupdate.php?q=" + str, true);
     xmlhttp.send();
  }
  </code></pre>
  
  <p>I think the problem comes because my form ajaxupdate.php file doesn't retrieve the First and Last name values from the form. It's like I am not passing them (?).</p>
  
  <p>Here is my ajaxupdate.php file:</p>
  
  <pre><code>&lt;?php   include("connection.php");
  
  $id=$_GET['ud_id'];
  $first=$_GET['ud_first'];
  $last=$_GET['ud_last'];
  
  
  $query="UPDATE contacts SET first='$first', last='$last' WHERE id='$id'";
  
  
  mysql_query($query);
  mysql_close();
  ?&gt;
  </code></pre>
  
  <p><strong>What I'm I doing wrong so that I can update the value first and last of database for a specific ID ?</strong></p>