title: Update value in MySQL via AJAX/PHP when a dynamically created checkbox is clicked?
body: <p>I am creating an admin panel for a project that I am working on. It will list a bunch of entries in a table and each row will have a checkbox in it. This checkbox will be used to activate an entry to be displayed on the website. </p>
  
  <p>I am setting the <code>id</code> and <code>name</code> of the checkbox with data from the MySQL database. For example..</p>
  
  <pre><code>&lt;input type="checkbox" class="active" name="active&lt;?php echo $id; ?&gt;" id="active&lt;?php echo $id; ?&gt;" &lt;?php if ($active == 1): ?&gt;checked="checked"&lt;?php endif; ?&gt; value="&lt;?php echo $id; ?&gt;"&gt;
  </code></pre>
  
  <p>For the entry with ID of 5 it will look like this..</p>
  
  <pre><code>&lt;input type="checkbox" class="active" name="active5" id="active5" checked="checked" value="5"&gt;
  </code></pre>
  
  <p>I need to set this up so that when you check a box or uncheck it that it updates the "active" value in the database. How do I grab the value of each checkbox, when clicked, and send that value to the MySQL database. I can do this easily if I know the checkboxes <code>name</code> beforehand, but since the <code>name</code> is partly generated from the database I'm not sure how to write the code to determine which entry gets the active value. </p>
  
  <p><strong>Here is my jQuery..</strong></p>
  
  <pre><code>$("input.active").click(function() {
  // store the values from the form checkbox box, then send via ajax below
  var check_active = $(this).is(':checked') ? 1 : 0;
  var check_id = $(this).attr('value');
  
  console.log(check_active);
  console.log(check_id);
  
      $.ajax({
          type: "POST",
          url: "http://nowfoods.marketspacecom.com/nextstep/ajax.php",
          data: {id: check_id, active: check_active},
          success: function(){
              $('form#submit').hide(function(){$('div.success').fadeIn();});
  
          }
      });
  return true;
  });
  </code></pre>
  
  <p><strong>Here is the PHP..</strong></p>
  
  <pre><code>&lt;?php
  
  include("dbinfo.inc.php");
  mysql_connect($server,$username,$password);
  @mysql_select_db($database) or die( "Unable to select database"); 
  
  // CLIENT INFORMATION
  $active = mysql_real_escape_string($_POST['active']);
  $id = mysql_real_escape_string($_POST['id']);
  
  $addEntry = "UPDATE entries SET active = '$active' WHERE id = '$id'";
  mysql_query($addEntry) or die(mysql_error());
  
  
  mysql_close();
  </code></pre>
  
  <p>?></p>
  
  <p><img src="https://i.stack.imgur.com/zGkFu.png" alt="Screenshot of what I am building"></p>