【问题标题】:mysql db update using multiple checkboxmysql db更新使用多个复选框
【发布时间】:2016-08-20 06:05:44
【问题描述】:

我可以通过 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 的脚本。

【问题讨论】:

标签: php jquery mysql ajax checkbox


【解决方案1】:

我已经更新了答案:(我会建议你使用 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应该是一样的。

【讨论】:

  • 谢谢回复,我可以在 中使用id="switch[]" 吗?
  • 我是 ajax 和 php 新手,你能帮我发一下代码吗?
  • 没有 id 必须是唯一的...你的id 没问题,只需更改name 将所有复选框的name ='switch[]' 放在一起
  • 应该对我的 javascript 进行哪些更改?
  • 举个例子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
相关资源
最近更新 更多