0

I'm looking to auto submit when a specific checkbox is checked.

I need it to pass to ajax.

Here is what I have so far:

$(document).ready(function () {
    $("input[name=<?php echo("$newseo"); ?>]").click(function(){
    var id=$(this).attr('id');
    var favorite=$(this).val();
    $.ajax({
            type:'POST',
            url:'check_favorite.php',
            data:'id= ' + id + '&favorite='+favorite
        });
    }
    });
});

But I just can't seem to get it to work,

Any help would be great, Thanks!

3
  • Just for kicks, in your jQuery selector for the checkbox, select it by ID rather than by name. $('#yourcheckboxid') Commented Jul 11, 2011 at 15:19
  • what does your html look like? Commented Jul 11, 2011 at 15:21
  • HTML: <input name="<?php echo("$newseo"); ?>" value="yes" type="checkbox" id="<?php echo("$newseo"); ?"> Commented Jul 11, 2011 at 15:22

4 Answers 4

1

Here you go, this should do it. Your AJAX looks fine. I put together a JSFiddle to demonstrate.

$("input[name=TestCheck]:checked").live('click', function(e) {
    var id=$(this).attr('id');
    var favorite=$(this).val();
        alert(id + " - " + favorite);
       // Post here ...
       $.ajax({
        type:'POST',
        url:'check_favorite.php',
        data: {id: id, favorite: favorite}
    });

});

JSFiddle : http://jsfiddle.net/4GQ6K/1/

I don't really like using obtrusive JavaScript and inputting PHP into JavaScript like that but there is no reason it shouldn't work.

Sign up to request clarification or add additional context in comments.

Comments

0
$('#my_checkbox').change(function(){
    if($(this).is(':checked'))
    {
        $('#my_form').submit();
    }
});

Comments

0

Use your server side code[php] to set a id or a class to your specific checkbox. Then bind a click event to the given class name, e.g.

php code sets a class named .sCheckBx.

then on document.ready bind your event :

$(document).ready(function () {
    $(".sCheckBx").click(function(){
    var id=$(this).attr('id');
    var favorite=$(this).val();
    $.ajax({
            type:'POST',
            url:'check_favorite.php',
            data:'id= ' + id + '&favorite='+favorite
        });
    }
    });
});

Comments

0

Try this

$(document).ready(function () {
    $("checkboxId").click(function(){
    var $this = $(this);
    if($this.is(":checked")){
      var id=$this.attr('id');
      var favorite=$this.val();
      $.ajax({
            type:'POST',
            url:'check_favorite.php',
            data:'id= ' + id + '&favorite='+favorite
        });
     } 
    }
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.