Jump to content

How to check checkboxes???

- - - - -

  • Please log in to reply
3 replies to this topic

#1
justsachin4u

justsachin4u

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Suppose we have 3 checkboxes in a form. For example for 3 hobbies -
1. reading
2. writing
3. playing

The values of these three checkboxes are stored in a database table under a column hobbies. When registered user goes to form in future for editing purpose, then we want his chosen hobbies are pre-selected.

When we stored these 3 hobbies in database then we used a function implode() to join the array elements into a string. For example, in formdesg.php page -


<input type="checkbox" name="hobbies[]" id="reading" value="Reading">Reading

</label>

<label for="writing">

<input type="checkbox" name="hobbies[]" id="writing" value="Writing">Writing

</label>

<label for="playing">

<input type="checkbox" name="hobbies[]" id="playing" value="Playing">Playing


And in the formprocess.php page - we used

$hobbies=implode(", ",$_REQUEST["hobbies"]);

And in the formdesg.php page, we retrieved checkboxes value as


$hobbies=$_REQUEST["hobbies"]?explode(", ",$_REQUEST["hobbies"]):"";


And we tick our checkboxes which user had chosen when he was registered as -


<input type="checkbox" name="hobbies[]" id="reading" value="Reading" <?php if($hobbies[0]=="Reading") echo "checked" ?>>Reading

</label>

<label for="writing">

<input type="checkbox" name="hobbies[]" id="writing" value="Writing" <?php if($hobbies[1]=="Writing") echo "checked" ?>>Writing

</label>

<label for="playing">

<input type="checkbox" name="hobbies[]" id="playing" value="Playing" <?php if($hobbies[2]=="Playing") echo "checked" ?>>Playing


Now the problem arises is that, if the user selected all the 3 checkboxes, then the above trick works very well. But if he selected one or any two checkboxes then the checked checkboxes are not those which he selected at the time of registration. I tell you why this happens -

when all the checkboxes are checked then the code


$hobbies=$_REQUEST["hobbies"]?explode(", ",$_REQUEST["hobbies"]):"";


returns

$hobbies[0] = reading;
$hobbies[1] = writing;
$hobbies[2] = playing;

but if all the checkboxes were not chosen then the above order shuffles and checkboxes are not checked like they were.

So tell me frdz how to solve this problem???

#2
Revolt

Revolt

    Programmer

  • Members
  • PipPipPip
  • 99 posts
You are depending on the order of the hobbies and, as you have already noticed, this is what is causing the problem. The quickest way to solve it would be using the array_search function to check if a specific value is on the hobbies array or not: PHP: array_search - Manual

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
In your form data, why not assign each hobby its own variable name, instead of the hobby array? That way, you can access each hobby by name and not have an ordering issue.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
justsachin4u

justsachin4u

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
how to use function array_search(). It will also create a problem. Because if some user has not selected any checkbox then this function's 2nd argument will be empty.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users