weixin_33712881 2015-10-22 07:39 采纳率: 0%
浏览 36

如果在php中选中了复选框

I have use the following code

order.php

        <script type="text/javascript">
        function processForm() { 
        $.ajax( {
            type: 'POST',
            url: 'ajax.php',
            data: { checked_box : $('input:checkbox:checked').val()},

            success: function(data) {
                $('#results').html(data);
            }
        } );
        }
        </script>

        <input type="checkbox" name="checked_box" value="1" onclick="processForm()">

And ajax.php

<?php
include "config.php";
$checkbox = intval($_POST['checked_box']);
echo $checkbox;
if($checkbox == 1){
    $Query="SELECT * FROM `order` ORDER BY sl_no DESC";
}else{
    $Query="SELECT * FROM `order` ORDER BY sl_no ASC";    
}
$result=mysql_query($Query);
echo $Query;
while($row = mysql_fetch_object($result)) { 
?>

It works good but when I unchecked then error showing

Notice: Undefined index: checked_box in C:\xampp\htdocs\website\admin\ajax.php on line 24

How to avoid this error???

  • 写回答

4条回答 默认 最新

  • weixin_33701294 2015-10-22 07:44
    关注

    It is not an error, but a notice. You will get that kind of notices if you try to access non initialized variables. Just initialize it with the appropiate start value.

    The tricky part in your code is that you assume you'll get the value of the checkbox because it is included in the form. But it happens that the checkbox are not sent UNLESS THEY HAVE BEEN CHECKED.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀