sloth456 Posted October 25, 2009 Share Posted October 25, 2009 I've tried googling it and I've looked around on the forum but I have another annoying checkbox problem. Say I have 3 checkboxes. They directly effect the data that is pulled from a database and placed on the same page. (the form and data are on the same page). By default all 3 boxes are checked. On the frontend I've just added the "checked=yes" attribute to each checkbox so they appear checked. On the backend I've just coded the following $value1="checked"; $value2="checked"; $value3="checked"; Trouble is when I uncheck the boxes the code doesn't process it properly. Since the checkbox's don't actually send a 'false' value, they just send nothing at all, so I use isset() to determine whether they are checked or not, like so. $value1="checked"; $value2="checked"; $value3="checked"; if(!isset($_GET['checkbox1']) { $value="unchecked"; } if(!isset($_GET['checkbox2']) { $value2="unchecked"; } if(!isset($_GET['checkbox3']) { $value3="unchecked"; } This doesn't work because when the page is first loaded the GET variables are not set, so the script puts all my $values to "unchecked" when I want them to remain "checked". I know I've described it in a bit of a confusing way. If you need any clarification just ask and I'll do my best. Quote Link to comment Share on other sites More sharing options...
smerny Posted October 25, 2009 Share Posted October 25, 2009 there is no "unchecked=yes" just use $value1 = ""; if(isset($_GET['checkbox1']) { $value1="checked=\"checked\""; } <input type="checkbox" name="checkbox1" value="whatever" <?php echo $value1; ?> /> Quote Link to comment Share on other sites More sharing options...
sloth456 Posted October 25, 2009 Author Share Posted October 25, 2009 I don't think you understood what I'm trying to do. Lets reduce to just one checkbox to make things simple. I want $value to be '1' when the checkbox is checked. '0' when the checkbox is unchecked. And '1' when the page first loads. Perhaps I'm being stupid, but I can't find a simple way to do that. Quote Link to comment Share on other sites More sharing options...
smerny Posted October 25, 2009 Share Posted October 25, 2009 well that's not even what you were attempting to do above, you were assigning either "" or "checked", but if you want to do 1 or 0, do it like this i guess: $value1 = 0; if(isset($_GET['checkbox1']) { $value1=1; } // within the form: <input type="checkbox" name="checkbox1" value="whatever" <?php ($value == 1 ? echo "checked=\"checked\"") ?> /> Quote Link to comment Share on other sites More sharing options...
sloth456 Posted October 26, 2009 Author Share Posted October 26, 2009 Smerny, I know I was assigning checked and unchecked before, I changed it to make it easier to understand. Thanks for all your helpso far, it's really appreciated, however in the code you've given me when the page first loads $value1 will be set to 0. It needs to be 1. Quote Link to comment Share on other sites More sharing options...
smerny Posted October 26, 2009 Share Posted October 26, 2009 my code will make value1 equal 0 if it has not been checked and 1 if it has been Quote Link to comment Share on other sites More sharing options...
sloth456 Posted October 26, 2009 Author Share Posted October 26, 2009 I know that. But when the page first loads $value will be set to '0'. I want it to be set to '1'. Quote Link to comment Share on other sites More sharing options...
smerny Posted October 26, 2009 Share Posted October 26, 2009 it doesn't make any difference to the end result, and to me it makes more sense to have it set to off and turn it on... but if you want to do it the other way, just invert everything in that part... from $value1 = 0; if(isset($_GET['checkbox1']) { $value1=1; } to $value1 = 1; if(!isset($_GET['checkbox1']) { $value1=0; } Quote Link to comment Share on other sites More sharing options...
sloth456 Posted October 26, 2009 Author Share Posted October 26, 2009 Now when the page first loads $value1 will still be set to 0. When the page first loads the $_GET variable will not be set. Thus the !isset() clause returns true and $value becomes 0. I've tried all these things myself, this is precisely the problem. Quote Link to comment Share on other sites More sharing options...
smerny Posted October 26, 2009 Share Posted October 26, 2009 you want the checkboxes to be checked to initially? then instead of (!isset($_GET['checkbox1'])), use ($_GET['checkbox1'] != 'whateverValue') Quote Link to comment Share on other sites More sharing options...
sloth456 Posted October 26, 2009 Author Share Posted October 26, 2009 Thanks for all your help smerny. That last piece of code didn't do the job. Instead I've found a way in my script of avoiding this pproblem all together by not having to have the boxes checked by default. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.