title: AJAX update MYSQL database using function called from HTML generated from PHP
body: <p>I have a php page generating and displaying a table. for the last row in the table i want to display an image with an 'onclick' function attached. this will send the username for the selected row to a script that will use AJAX to update a database. The table displays fine but the AJAX is not working. my php to display the image is:</p>
  
  <pre><code>echo "&lt;td&gt; &lt;img id='tblimg' 
      onclick='like('" . $row['Username'] . "')' 
      src='like.jpg' alt='like/dislike image' 
      width='80px' height='30px'&gt;&lt;/td&gt;";
  </code></pre>
  
  <p>The javascript function is:</p>
  
  <pre><code>&lt;script type="text/javascript" &gt;
  
          function like(user) 
          {
  
              $.ajax(
                  url: "update.php",
                  type: "POST",
                  data: { 'username': user, 'liked': '1' },                   
                  success: function()
                              {
                                  alert("ok");                                    
                              }
              );
          }
  
  &lt;/script&gt;       
  </code></pre>
  
  <p>And here is update.php:</p>
  
  <pre><code>&lt;?php
  
  $con=mysqli_connect("","sam74","********","sam74");
  
  // Check connection
  if (mysqli_connect_errno())
  {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  
  $Username = $_POST['username'];
  $Liked = $_POST['liked'];   
  
  $sql = "UPDATE 'followers' SET 'Liked' = '$Liked' WHERE 'Username' = '$Username'";
  
  if (!mysqli_query($con,$sql))
  {
      die('Error: ' . mysqli_error($con));
  }
  
  mysqli_close($con);
  </code></pre>
  
  <p>?></p>