1

I am onto this problem. During check of the following code in web page, the checkBoxClicked function is called.

<input type="checkbox" name="check" onchange="checkBoxClicked(this)"/>Check

But I sometime need to check this checkbox using php (during editing process). So while I set this check box checked using this code

<input type="checkbox" name="check" onchange="checkBoxClicked(this)"
    <?php if($check =='1'){
    echo "checked=\"true\"";
    }?>
/>Check

I want the function to be called.

What can I do to get the function called when the checkbox is checked using php?

1 Answer 1

2
<input type="checkbox" name="check" onchange="checkBoxClicked(this)"
<?php if($check =='1'){
echo "checked=\"checked\"";
}?>
/>Check

<?php if($check == '1'){
    echo '<script type="text/javascript">checkBoxClicked(document.getElementsByName("check")[0]);</script>';
}
?>

This would be the quick n dirty solution

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

5 Comments

Isn't there any method that I can embed inside the input field?
'checked="true"' should be 'checked="checked"' for compatibility reasons
you are right. I've just copied the code from above and assumed that it is correct :)
Tailing the question. pastebin.com/BaRVm4bA This is the part I am trying to do. When checked manually, I get the div id="check" displayed. But when done through the code, I get error message saying document.getElementById("check") is null where am I going wrong?
the code is executed before the <div id="check"></div> part is loaded. So javascript can't access document.getElementById('check') because this part is not yet present. In this case you have to put the echo from the php at the bottom of your script or have it executed after the page is loaded

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.