Jump to content

Keep checkbox checked after submit refresh


user4245
Go to solution Solved by taquitosensei,

Recommended Posts

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?

Link to comment
Share on other sites

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"
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • Solution

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?

Link to comment
Share on other sites

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[]"...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.