Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checkbox on change ajax call

I have one checkbox. I want when I checked the checkbox so I get the 1 and then update a mysql query through that get value. I also want if I unchecked the checkbox so I get a value 0 so then I again update the mysql query. Help me. it should be done with ajax call. code will be in PHP.

HTML code

<input type="checkbox" name="action1" id="action1" title="Action 1" value="1" onclick="return Populat_Industry('set_home_vid.php');"/>

ajax call

<script>
function Populat_Industry(url){
    var value=$(#action1).val();
    $.ajax({
   type: "POST",
   url: url,
   async: true,
   data: "value="+value,
   success: function(msg){
      //alert('Success');
       if(msg !='success'){
        //alert('Fail');
       } 
   }
});
}

</script>

PHP code

if($_POST['action1']=='1'){
$query= mysql_query("UPDATE homevideos SET is_active = '1
}
else{
mysql_query("UPDATE homevideos SET is_active = '0')
echo 'success';
like image 854
Muhammad Atiq Avatar asked Sep 26 '14 17:09

Muhammad Atiq


1 Answers

Ajax call is async. you cannot use return with it this way. Write checkbox change event in jquery and send ajax call.

Do like this:

HTML:

<input type="checkbox" name="action1" id="action1" title="Action 1" value="1"/>

JQUERY:

$("#action1").change(function () {
    var value = $(this).val();
    $.ajax({
        type: "POST",
        url: "set_home_vid.php",
        async: true,
        data: {
            action1: value // as you are getting in php $_POST['action1'] 
        },
        success: function (msg) {
            alert('Success');
            if (msg != 'success') {
                alert('Fail');
            }
        }
    });
});
like image 78
Ehsan Sajjad Avatar answered Sep 30 '22 06:09

Ehsan Sajjad