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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配