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.

    评论

报告相同问题?