jeudi 22 octobre 2015

If checkbox is checked in 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???




Aucun commentaire:

Enregistrer un commentaire