title: PHP - Deleting data with buttons
body: <p>I'm trying to create a HTML table that lists all the rows in a database table. Then, next to each row I want to have a button so the user can delete that entry. I have created the table but I can't get the buttons to work.</p>
  
  <p>I have been searching around and I found this post <a href="https://stackoverflow.com/questions/20738329/how-to-call-a-php-function-on-the-click-of-a-button">How to Call a PHP Function on the Click of a Button</a> but I can't get it to work. I've never used ajax before so I might not understand the code correctly.</p>
  
  <p>Here is the code:</p>
  
  <p><strong>Go through all the data from the table and create a button for each entry</strong></p>
  
  <pre><code>&lt;?php
  for ($x = 0; $x &lt; sizeof($data); $x++) {
  ?&gt;
       &lt;input type="submit" class="tableButton" name="&lt;?php echo $x ?&gt;" value="&lt;?php echo $x ?&gt;"&gt;
  &lt;?php
  }    
  ?&gt;
  </code></pre>
  
  <p><strong>When a tableButton is clicked, send its value to ajax.php</strong></p>
  
  <pre><code>$('.tableButton').click(function () {
      var clickBtnValue = $(this).val();
      var ajaxurl = 'ajax.php',
      data = { 'action': clickBtnValue };
      $.post(ajaxurl, data, function (response) {
      });
  });
  </code></pre>
  
  <p><strong>ajax.php</strong></p>
  
  <p><strong>Get the value of the button that was pressed and do something with it</strong></p>
  
  <pre><code>&lt;?php
  if (isset($_POST['action'])) {
      $data = $_POST['action'];
      echo $data;
  }
  ?&gt;
  </code></pre>
  
  <p>Currently I just have it echo the value to test it but it's displaying nothing. What I would have it do is run this query:</p>
  
  <pre><code> DELETE from myTable WHERE id = $data;
  </code></pre>
  
  <p>Or if someone knows a better way to do this please let me know.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>After doing a lot more searching I found out why this wasn't working how I expected. As I suspected since I've never used AJAX before there was something I missed, I didn't know the echo wouldn't print directly to the screen. I just changed the echo to a delete query and tested that and it works... So the code is fine, but I think I should learn AJAX sometime. Thanks for all the responses.</p>
  
  <p>I'm also aware of the sql injection that is possible here, this was just a quick mock-up, thanks.</p>