0

hello I have problem in reading and sending checkbox value I tried using ajax but it didn't work what the wrong with my ajax????!!! " what I really need is to retrieve the value " . $row['ID'] . " and write statement to insert value to the db in case it was checked ?!"

function defaultt(id) {
        $.ajax({

            type : "POST",
            data : "id=" + id,
            url : "defaultt.php",
            success : function(msg) {
                $('#' + id).fadeOut(300);
            }
        });

code:

<td><form><input type="checkbox" name="state" id="state"class="check"  checked onClick='javascript:defaultt(" . $row['ID'] . ");' >

retrieve code in other page.php:

<?php $id1 = intval($_POST['id']);

if (isset($id1)) {} ?>

I also used this code but didn't work :(

    <?php if ($_POST['state']){?>
 <script type=text/javascript>
function validate(){
if(state.checked == 1){
      alert("checked") ;}else{
 alert("You didn't check it! Let me check it for you.")
 }
 }
 </script>
  <?php}?>
5
  • 2
    What does 'it didn't work' mean? An error? The wrong data? Some of the right data? Too much data? Commented Oct 11, 2013 at 12:22
  • 1
    you sent id "id=" not state $_POST['state'] Commented Oct 11, 2013 at 12:26
  • In the first version of your function you pass in an id but don't ever check the current checked state of the checkbox. Note also that you don't need the javascript: label in an inline event attribute. Commented Oct 11, 2013 at 12:33
  • yah sorry I should write: $id1 = intval($_POST['id']); if (isset($id1)) but still not work :( Commented Oct 11, 2013 at 12:43
  • mr.nnnnn how can I check if it checked or not? Commented Oct 11, 2013 at 15:59

3 Answers 3

1

Try this

    function validate(){
    if($("#state").is(":checked")){
        alert("checked") ;
      }
      else{
        alert("You didn't check it! Let me check it for you.")
    }
 }

Demo

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

2 Comments

$("#state").prop('checked') is an alternative
thanks for your time but I put this function in the header section but didn't work and what I really need is to retrieve the value " . $row['ID'] . " :( any idea?
0

HTML

<input type="checkbox" name="check" id="check"/>

JS

$('#check').change(function() {

    var $check = $(this),
        $div = $check.parent();

    if ($check.prop('checked')) {
        alert("Check");
        $div.addClass('test');

    } else {
        alert("Uncheck");
        $div.removeClass('test');

    }

});

demo

1 Comment

Why use $check.prop('checked') when you can just say this.checked?
0
To check Checkbox true: ($('#id').prop('checked') == true)
$('#state').on('click',function(){

if ($('#state').prop('checked') == true)
  {
 alert('checked');
  }
    else
      { alert('Not checked'); }
});

Demo

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.