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???


Sign In
Create Account


Back to top









