title: Store Value from On/Off Button in MySQL Database Using PHP Ajax
body: <p>I found a tutorial for this with single ID. <a href="http://www.freezecoders.com/2013/05/store-value-from-onoff-button-in-mysql-database-using-php-ajax.html#comment-2764" rel="nofollow">enter link description here</a></p>
  
  <p>i have modified as per my knowledge for multiple id, i cannot able to get the id using ajax. i am not good with ajax. I have attached both code. can anyone tell how i can fix it for multiple id</p>
  
  <p>i also attached a screen shot <a href="http://take.ms/7cNqf" rel="nofollow">screenshot</a></p>
  
  <p>index.php</p>
  
  <pre><code>    &lt;?php
  $query=mysql_connect("localhost","pma","pmapass");
  mysql_select_db("testdb",$query);
  include("connection1.php");
  $result = mysql_query("SELECT * FROM mytable ORDER BY id");
  ?&gt;
  &lt;script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"&gt;
  &lt;/script&gt;
  
   &lt;div&gt;
                &lt;table id="datatables" class="display"&gt;
                  &lt;thead&gt;
                    &lt;tr&gt;
                      &lt;th&gt;ID&lt;/th&gt;
                      &lt;th&gt;Name&lt;/th&gt;
                      &lt;th&gt;Text&lt;/th&gt;
                    &lt;/tr&gt;
                  &lt;/thead&gt;
                 &lt;tbody&gt;
                    &lt;?php
                      while ($row = mysql_fetch_array($result)) {
                          ?&gt;
                    &lt;tr&gt;
                      &lt;td&gt;&lt;?php echo $row["id"]; ?&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;?php echo $row["name"]; ?&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;?php echo $row["text"]; ?&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;script type="text/javascript"&gt;
  $(document).ready(function(){
  $('#myonoffswitch&lt;?php echo $row["id"]; ?&gt;').click(function(){
  var myonoffswitch&lt;?php echo $row["id"]; ?&gt;=$('#myonoffswitch&lt;?php echo $row["id"]; ?&gt;').val();
  if ($("#myonoffswitch&lt;?php echo $row["id"]; ?&gt;:checked").length == 0)
  {
  var a&lt;?php echo $row["id"]; ?&gt;=myonoffswitch&lt;?php echo $row["id"]; ?&gt;;
  }
  else
  {
  var a&lt;?php echo $row["id"]; ?&gt;="off";
  }
  
  $.ajax({
  type: "POST",
  url: "ajax.php",
  data: "value="+a&lt;?php echo $row["id"]; ?&gt;,
  success: function(html){
  $("#display").html(html).show();
  }
  });
  
  });
  });
  &lt;/script&gt;&lt;div class="onoffswitch"&gt;
  &lt;input type="hidden" value="&lt;?php echo $row["id"]; ?&gt;" name="ids" /&gt;
  &lt;input type="checkbox" name="onoffswitch&lt;?php echo $row["id"]; ?&gt;" class="onoffswitch-checkbox" id="myonoffswitch&lt;?php echo $row["id"]; ?&gt;"
  &lt;?php
  $query3=mysql_query("select * from mytable where id=$row[id]");
  $query4=mysql_fetch_array($query3);
  if($query4['text']=="off")
  {
  echo "checked";
  }
  ?&gt;&gt;
  &lt;label class="onoffswitch-label" for="myonoffswitch&lt;?php echo $row["id"]; ?&gt;"&gt;
  &lt;div class="onoffswitch-inner"&gt;&lt;/div&gt;
  &lt;div class="onoffswitch-switch"&gt;&lt;/div&gt;
  &lt;/label&gt;
  &lt;/div&gt;
  &lt;div id="display"&gt;&lt;/div&gt;
  &lt;/td&gt;
                    &lt;/tr&gt;
                    &lt;?php
                      }
                      ?&gt;
                  &lt;/tbody&gt;
                &lt;/table&gt;
              &lt;/div&gt;
  </code></pre>
  
  <blockquote>
    <p>Ajax.php</p>
  </blockquote>
  
  <pre><code>&lt;?php
  $query=mysql_connect("localhost","pma","pmapass");
  mysql_select_db("testdb",$query);
  if(isset($_POST['value']))
  {
  $value=$_POST['value'];
  $id=$_POST['ids'];
  mysql_query("update mytable set text='$value' where id=2");
  echo "&lt;h2&gt;You have Chosen the button status as:" .$value."&lt;/h2&gt;";
  }
  ?&gt;
  </code></pre>