jdadwilson Posted July 29, 2007 Share Posted July 29, 2007 I have a strange situation with Checkboxes that I need some assistance. On a form I can have a variable number of checkboxes depending on information supplied by the client. Additionally, the checkboxes will have unique “values” and “label” depending on client input. Because of the way the POST action handles checkboxes I want to ensure the passed state is set when the page is reloaded. First, in the login script I set the session variables, one for each checkbox like this... $longNameA = explode{“|”, “Choose One|Name1|Name2|Name3”); // Sample only $shortNameA = explode(“|”, “CO|N1|N2|N3”); // Sample only For ($i=1, $i<=count($shortNameA)-1, $i++) { $_SESSION[$shortNameA[$i]] = $shortNameA[$i]; } Next, in my subject script I check to see if the action is coming from the forms “update” input like this: if ($_POST['buttonID'] == "update") { For ($i=1, $i<=count($shortNameA)-1, $i++) { $_SESSION[$shortNameA[$i]] = (isset($_POST[groupCB[$shortNameA[$i]]) ? $_POST[groupCB[$shortNameA[$i]] : null); } // Set the $_POST values to the $_SESSION values For ($i=1, $i<=count($shortNameA)-1, $i++) { $_POST[groupCB[$shortNameA[$i]]] = $_SESSION[$shortNameA[$i]); } // Finally set the Checkboxes for ($i = 1; $i<=count($groupLongNameA)-1; $i++) { ?> <input type="checkbox" name="groupCB[]" value="<?php echo $groupShortNameA[$i]; ?>"<?php if ($_POST[$groupCB[$i]] == $groupShortNameA[$i]) { echo " checked"; }?>><?php echo $groupLongNameA[$i]; ?> <?php }?> I think the logic of what I am doing is sound. The problem is that the coding is not functioning the way I would like. Being new to PHP I am not sure how to create and check the POST and SESSION variables the way that is required. If you would like to see my work in progress send me an e-mail and I will provide you with the link. TIA for any assistance. Quote Link to comment Share on other sites More sharing options...
Dragen Posted July 29, 2007 Share Posted July 29, 2007 what's the problem you're having? Quote Link to comment Share on other sites More sharing options...
jdadwilson Posted July 30, 2007 Author Share Posted July 30, 2007 Couple... 1. When I initially load the page I would like to have all CBs checked. I have set the session variables during the login process to ensure this. This is not happening. 2. Once the state of a CB is changed any subsequent load of the page should reflect the last state. Thus, the reason for setting the session variables based on the post. Again, this is not happening. Thanks for your assistance. Quote Link to comment Share on other sites More sharing options...
Dragen Posted July 30, 2007 Share Posted July 30, 2007 I'm too tired to make much sense... sorry. But I think there are a couple of errors in your code, although the idea is good. I've actually just written a bit of code which should do near enough what you want.. I haven't tested it, As I've said I'm quite tired so hopefully I've not made too many mistakes <?php $longNameA = explode{“|”, “Choose One|Name1|Name2|Name3”); // Sample only $shortNameA = explode(“|”, “CO|N1|N2|N3”); // Sample only //I removed the code setting the session here, because I didn't think it was needed... ?> Next, in my subject script I check to see if the action is coming from the forms “update” input like this: <?php if($_POST['buttonID'] == "update"){ //a shorter method of going through each checkbox that's been set and saving it into an array I've named checks foreach($_POST['groupCB'] as $key => $value){ $_SESSION['checks'][] = $value; } } // Set the $_POST values to the $_SESSION values for($i=1, $i<=count($shortNameA)-1, $i++){ $_POST[groupCB[$shortNameA[$i]]] = $_SESSION[$shortNameA[$i]); } // Finally set the Checkboxes for($i = 1; $i<=count($groupLongNameA)-1; $i++){ //here I've changed the check to do a simple search for the checboxes value in the session array. //if it's in the array then it checks it echo '<input type="checkbox" name="groupCB[]" value="' . $groupShortNameA[$i] . '"'; if(in_array($groupShortNameA[$i], $_SESSION['checks'])){ echo ' checked="checked"'; } echo '>' . $groupLongNameA[$i] . ' '; } ?> I'm hoping that's all okay! Quote Link to comment Share on other sites More sharing options...
jdadwilson Posted July 30, 2007 Author Share Posted July 30, 2007 Thanks, I'll give it a try and let you know. Quote Link to comment Share on other sites More sharing options...
trq Posted July 30, 2007 Share Posted July 30, 2007 ps: Your using some wierd quotes. $longNameA = explode{“|”, “Choose One|Name1|Name2|Name3”); should be.... $longNameA = explode{"|", "Choose One|Name1|Name2|Name3"); Quote Link to comment Share on other sites More sharing options...
jdadwilson Posted July 30, 2007 Author Share Posted July 30, 2007 I think that comes from the word processor. The quotes are fine in Zend. Thanks... Quote Link to comment Share on other sites More sharing options...
jdadwilson Posted July 30, 2007 Author Share Posted July 30, 2007 Dragen; You are the man!!! Code worked great -- almost! I added an unset($_SESSION['checks']; statement before the initial FOREACH loop. Without it if you check an unchecked box it would stay checked. But if you unchecked a checked box the box would get checked on a reload. Additionally, I modified the one part that you removed (the part in the login script). Normally, I agree that CBs should not be checked. BUT, in this case I want them checked. So I added this code... $groupShortNameA = explode("|", $groupShortName); for ($i=1; $i<=count($groupShortNameA)-1; $i++) { $_SESSION['checks'][$groupShortNameA[$i]] = $groupShortNameA[$i]; } Thanks much for your assistance. The guys on this forum are the greatest... 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.