1

I have a php function script that is supposed to uncheck a checkbox or checkboxes if a user unchecks it when the preview button is clicked but I can only get the last checkbox that was unchecked to stay unchecked but not the other checkeboxes how can I fix this so that all the checkboxes that where unchecked stay unchecked?

Here is part of my PHP function that is giving me the problem.

if(isset($_POST['preview'])){
    foreach($query_cat_id as $qci) {
        if(!in_array($qci, $cat_id)){
            $unchecked = $purifier->purify(strip_tags($qci));
        }
    }
}


for ($x = 0; $x < count($query_cat_id); $x++){
    if(($query_cat_id[$x] == $cat['id']) && ($cat['id'] != $delete_id) && ($cat['id'] != $unchecked)){
        echo 'checked="checked"';
    }
}
1
  • @codaddict, Not all my checkboxes stay unchecked when unchecked is the problem. Commented Sep 25, 2010 at 7:00

2 Answers 2

1

Why not just just check if the variable is set, if the checkbox is checked when the form is submitted, it will be available in $_POST['checkboxName'], otherwise, isset($_POST['checkboxName']) will return false.

Basic script to test it

<?php
if (isset($_POST['heh']))
    echo $_POST['heh'];
else
    echo "Not checked";
?>
<form action='yourPage.php' method='post'>
<input type='checkbox' name='heh' />
<input type='submit' />
</form>

View it in action

http://robertsquared.com/so.php

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

6 Comments

I believed I tried this before but how can I integrate it into my code?
I'm not sure the overall functionally of what you're trying to achieve, just that you wanted to keep your checkboxes checked. Are you posting to the same file that submits like my example?
Not all my checkboxes stay unchecked when unchecked is the problem my script retrieves checkbox values from a database.
Alright, the preview had me thinking you were doing some sort of checking on form submit and you wanted to reproduce the checkbox's state. What are the values stored in the database for a checked checkbox vs an unchecked one?
The values that are stored in the database are all the checked checkboxes values from the last save to be displayed as a check in the checkboxes. And unchecked values are from a different table which displays all the checkboxes unchecked.
|
0

i solve this problem on client side

i have a input of type hidden with the same name as the checkbox with the value of 0 and the checkbox right after the hidden field with the value of 1

if the checkbox is not checked i get the value of 0 from the hidden field and if someone checks i got the 1 from the checkbox.

so i only need to check if $value==1 then checked=checked

2 Comments

Is there a better way to do this without using the hidden input tag.
i think there a lots of different ways, but for me, this is the simplest

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.