user4245 Posted July 11, 2013 Share Posted July 11, 2013 I am creating a clothing website and say for a color on the sidebar I want to be able to submit a form with 'onClick' that will refresh the page, which it already does but to keep the checkbox checked when doing so. Right now my code submits properly refreshing the page and displaying the right products but the check in the checkbox is gone. I need the check to stay. Here is my code $sidebar_color .= '<div id="sidebar"> <label><form action="?" method="REQUEST"> <input type="checkbox" name="color" value="'.$color.'" onClick="submit();" /> <font size="-2"> <a href="?color='.$color.'">'.$color.' ('.$row["COUNT(color)"].')</a> </font></form></label></div>'; Another problem is when the 'onClick="submit();"' refreshes the page and then I press back the checkbox is checked. I need it to be unchecked when I press back. Is this also possible? I have been at this for days and tried too many things to remember what I tried. I figure I need something like this if(isset($_REQUEST["color"])) echo "checked="checked"" but nothing seems to work for me. Anyone have any ideas how to fix the code? Quote Link to comment https://forums.phpfreaks.com/topic/280049-keep-checkbox-checked-after-submit-refresh/ Share on other sites More sharing options...
fastsol Posted July 11, 2013 Share Posted July 11, 2013 I have a tutorial on this type of thing, it deals with the select box but the theory is the same, just change it to use the check box instead and use checked="checked" http://amecms.com/article/Retaining-Drop-down-Selection-After-Form-Submit Quote Link to comment https://forums.phpfreaks.com/topic/280049-keep-checkbox-checked-after-submit-refresh/#findComment-1440263 Share on other sites More sharing options...
user4245 Posted July 11, 2013 Author Share Posted July 11, 2013 Thank you for the response. What do you mean by "just change it to use the check box instead"? Quote Link to comment https://forums.phpfreaks.com/topic/280049-keep-checkbox-checked-after-submit-refresh/#findComment-1440266 Share on other sites More sharing options...
web_craftsman Posted July 11, 2013 Share Posted July 11, 2013 Something like this: $checked = (isset($_REQUEST['color'])) ? 'checked' : ''; $sidebar_color .= ' ... <input type="checkbox" name="color" value="'.$color.'" onClick="submit();" ' . $checked . ' /> '; Quote Link to comment https://forums.phpfreaks.com/topic/280049-keep-checkbox-checked-after-submit-refresh/#findComment-1440269 Share on other sites More sharing options...
user4245 Posted July 11, 2013 Author Share Posted July 11, 2013 We are on the right track here. When I click the checkbox and the page refreshes all the checkboxes are checked now. How do I get it so just the type color I clicked on is checked? Any ideas? You are a huge help. Quote Link to comment https://forums.phpfreaks.com/topic/280049-keep-checkbox-checked-after-submit-refresh/#findComment-1440403 Share on other sites More sharing options...
cyberRobot Posted July 12, 2013 Share Posted July 12, 2013 It might help to see the updated code...including the other checkboxes. I suspect that you're using the same $checked variable for all the boxes. The problem is that variable is based on the "color" checkbox. Note: the same affect can be accomplished without the variable: <?php $sidebar_color .= ' ... <input type="checkbox" name="color" value="'.$color.'" onclick="submit();" ' . ((isset($_REQUEST['color'])) ? 'checked="checked"' : '') . ' /> '; ?> Since it looks like you're using XHTML, I changed the checked value to: checked="checked" Quote Link to comment https://forums.phpfreaks.com/topic/280049-keep-checkbox-checked-after-submit-refresh/#findComment-1440452 Share on other sites More sharing options...
user4245 Posted July 12, 2013 Author Share Posted July 12, 2013 cyberRobot, Yes your code does the same as web_craftsman. Here is the updated code $sidebar_color .= '<div id="sidebar"><label><form action="?" method="REQUEST"><input type="checkbox" name="color" value="'.$color.'" onClick="submit();" ' . ((isset($_REQUEST['color'])) ? 'checked="checked"' : '') . ' /><font size="-2"><a href="?color='.$color.'">'.$color.' ('.$row["COUNT(color)"].')</a></font></form></label></div>'; I think you are right with the problem being that the variable is based on the "color" checkbox. Is there are way to make it based on '.$color.'? The '.$color.' is all the individual colors in the database. I hope that make sense. Quote Link to comment https://forums.phpfreaks.com/topic/280049-keep-checkbox-checked-after-submit-refresh/#findComment-1440495 Share on other sites More sharing options...
cyberRobot Posted July 15, 2013 Share Posted July 15, 2013 Sorry, I'm not quite sure what you mean. Posting more of your code might be helpful, especially the code which pertains to the $color variable. Quote Link to comment https://forums.phpfreaks.com/topic/280049-keep-checkbox-checked-after-submit-refresh/#findComment-1440763 Share on other sites More sharing options...
user4245 Posted July 16, 2013 Author Share Posted July 16, 2013 Is there a way to make to checkbox react to the form value which is '.$color.' instead of the form name which is color. When I submit right now the code will check all of the colors fetched from the database but I want it to only check the individual color it clicked on. Here is my code. <?php $sidebar_color =""; $color_sql = mysql_query("SELECT *,COUNT(color) FROM products GROUP BY color ORDER BY color ASC"); $coCount = mysql_num_rows($color_sql); if ($coCount > 0) { while($row = mysql_fetch_array($color_sql)){ $color = $row["color"]; $sidebar_color .= '<div id="sidebar"><label><form action="?" method="REQUEST"><input type="checkbox" name="color" value="'.$color.'" onClick="submit(); return false" ' . (isset($_REQUEST['color']) ? 'checked="checked"' : '') . ' /><font size="-2"><a href="?color='.$color.'">'.$color.' ('.$row["COUNT(color)"].')</a></font></form></label></div>'; } } ?> My page will look something like this checkbox black (3) checkbox yellow (5) checkbox white (9) ... All the pages are different and vary all the time so I can't individually write out the colors in the form which is why I use '.$color.' I hope this is a little more clear. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/280049-keep-checkbox-checked-after-submit-refresh/#findComment-1440947 Share on other sites More sharing options...
Solution taquitosensei Posted July 16, 2013 Solution Share Posted July 16, 2013 Try this. You need to check if color is set and if it matches the color of the row you're on in the loop. (isset($_REQUEST['color']) && $_REQUEST['color']==$row['color']) ? 'checked="checked"' : '' Is there a way to make to checkbox react to the form value which is '.$color.' instead of the form name which is color. When I submit right now the code will check all of the colors fetched from the database but I want it to only check the individual color it clicked on. Here is my code. <?php $sidebar_color =""; $color_sql = mysql_query("SELECT *,COUNT(color) FROM products GROUP BY color ORDER BY color ASC"); $coCount = mysql_num_rows($color_sql); if ($coCount > 0) { while($row = mysql_fetch_array($color_sql)){ $color = $row["color"]; $sidebar_color .= '<div id="sidebar"><label><form action="?" method="REQUEST"><input type="checkbox" name="color" value="'.$color.'" onClick="submit(); return false" ' . (isset($_REQUEST['color']) ? 'checked="checked"' : '') . ' /><font size="-2"><a href="?color='.$color.'">'.$color.' ('.$row["COUNT(color)"].')</a></font></form></label></div>'; } } ?> My page will look something like this checkbox black (3) checkbox yellow (5) checkbox white (9) ... All the pages are different and vary all the time so I can't individually write out the colors in the form which is why I use '.$color.' I hope this is a little more clear. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/280049-keep-checkbox-checked-after-submit-refresh/#findComment-1440960 Share on other sites More sharing options...
cyberRobot Posted July 17, 2013 Share Posted July 17, 2013 Is there a reason why your checkboxes have the same name? Are visitors only supposed to check one color? If so, that's what radio buttons are for. If visitors can select multiple colors, you should rename the fields to something like <input type="checkbox" name="color_'.$color.'"... Or you could utilize an array <input type="checkbox" name="color[]"... Quote Link to comment https://forums.phpfreaks.com/topic/280049-keep-checkbox-checked-after-submit-refresh/#findComment-1441048 Share on other sites More sharing options...
user4245 Posted July 17, 2013 Author Share Posted July 17, 2013 Thank you for all your help everyone especially you cyberRobot. taquitosensei your code worked perfectly. Thank you so much!! Quote Link to comment https://forums.phpfreaks.com/topic/280049-keep-checkbox-checked-after-submit-refresh/#findComment-1441109 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.