mysql db update使用多个复选框

时间:2016-04-26 12:05:07

标签: php jquery mysql ajax checkbox

我可以通过ajax将一个复选框值0/1更新为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 个答案:

答案 0 :(得分: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应该是一样的。