2016-04-26 137 views
-3

我可以通過AJAX一個複選框值0/1更新MySQL數據庫,但我不知道如何與多個複選框爲此,MySQL數據庫更新使用多個複選框

我的代碼:的index.php

<?php 
$query=mysql_connect("localhost","root","root"); 
mysql_select_db("gpio",$query); 
?> 

<!DOCTYPE html> 
<html> 
<head> 
<title>Checkbox Switches DevGrow.com</title> 
<script type="text/javascript"src="jquery.min.js"> 

</script> 

    <script type="text/javascript"> 
     $(document).ready(function(){ 
      $('#switch1').click(function(){ 
       var myonoffswitch=$('#switch1').val(); 
       if ($("#switch1:checked").length == 0) 
       { 
        var a="1"; 
       } 
       else 
       { 
        var a="0"; 
       } 

       $.ajax({ 
        type: "POST", 
        url: "ajax.php", 
        data: "value="+a , 
        success: function(html){ 
         $("#display").html(html).show(); 
        } 
       }); 
      }); 
     }); 
    </script> 
</head> 

<body> 
    <input type="checkbox" name="switch1" id="switch1" 
     <?php 
      $query3=mysql_query("select pinDescription from pindescription where pinID=1"); 
      $query4=mysql_fetch_array($query3); 
      if($query4['pinDescription']=="0") 
      { 
       echo "checked"; 
      } 
     ?> > 
</body> 
</html> 

ajax.php

<?php 
$query=mysql_connect("localhost","root","root"); 
mysql_select_db("gpio",$query); 
if(isset($_POST['value'])) 
{ 
$value=$_POST['value']; 
mysql_query("update pindescription set pinDescription='$value' where pinID='1'"); 
} 
?> 

上述代碼的工作只爲一個複選框,以什麼爲8或10複選框做。

<input type="checkbox" name="switch1" id="switch1" 
     <?php 
      $query3=mysql_query("select pinDescription from pindescription where pinID=1"); 
      $query4=mysql_fetch_array($query3); 
      if($query4['pinDescription']=="0") 
      { 
       echo "checked"; 
      } 
     ?> > 

<input type="checkbox" name="switch2" id="switch2" 
     <?php 
      $query3=mysql_query("select pinDescription from pindescription where pinID=2"); 
      $query4=mysql_fetch_array($query3); 
      if($query4['pinDescription']=="0") 
      { 
       echo "checked"; 
      } 
     ?> > 

<input type="checkbox" name="switch3" id="switch3" 
     <?php 
      $query3=mysql_query("select pinDescription from pindescription where pinID=3"); 
      $query4=mysql_fetch_array($query3); 
      if($query4['pinDescription']=="0") 
      { 
       echo "checked"; 
      } 
     ?> > 

如何在switch1 switch2 switch3的腳本中進行更改。

+1

你的sql很容易被mySql注入。使用PDO。 –

+0

更不用說老語法 – madalinivascu

+0

@bub,你能告訴我如何處理PDO嗎? – nish

回答

0

我已經更新了答案:(我會建議你使用mysqli_ (),爲mysql_()折舊,但我並沒有改變有關的事情。)

<!DOCTYPE html> 
<html> 
<head> 
<title>Checkbox Switches DevGrow.com</title> 
<script type="text/javascript"src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.js"> 

</script> 

    <script type="text/javascript"> 

     $(document).ready(function(){ 

      $('input[name="switch"]').on("click", function() { 
       if($(this).is(":checked")){ 

        //alert("Checkbox is checked." + $(this).val()); 

        $.ajax({ 
         type: "POST", 
         url: "ajax.php", 
         data: "value=1&id"+$(this).val() , 
         success: function(html){ 
          $("#display").html(html).show(); 
         } 
        }); 

       } 
/*below else block is not required but if user uncheck any checkbox & you want to update the description in db you can call ajax & update DB from this else part */ 
       else if($(this).is(":not(:checked)")){ 

        // alert("Checkbox is unchecked."+ $(this).val()); 

       } 
      }); 


     }); 
    </script> 
</head> 

<body> 
<?php 

    /* If you are confirmed that how many checkbox you need to show, you can do it from database.For the time being i have used a for loop to create checkbox dynamically. 
$query=mysql_query("select pinDescription,pinID from pindescription where pinID between 1 an 10"); 
    ; 
    while($rs=mysql_fetch_array($query)) 
    { 
     $ret[$rs["pinID"]] = $rs['pinDescription']; 
    } 
      ?> > 
    */ 
    $ret = array(1=>0,2=>1,3=>1,4=>1,5=>0); 
    for($cnt=1;$cnt<5;$cnt++) 
    { 
    ?> 
     Switch<?php echo $cnt?><input type="checkbox" name="switch" id="switch<?php echo $cnt?>" <?php echo ($ret[$cnt] == 0) ? "checked" : "" ?> 
             value='<?php echo $cnt?>'> 

    <?php 
    } 
    ?> 
    </body> 
    </html> 

我已經改變只有你的索引頁面,你的ajax.php應該是一樣的。

+0

感謝您的回覆,我可以在中使用'id =「switch []」'? – nish

+0

我是ajax和php的新手,你能爲我發佈代碼嗎? – nish

+0

沒有ID必須是唯一的...你的'ID'是好的,只是改變'名稱'把所有的複選框的'名稱='開關[]'' –