Jump to content

retaining checkbox value after refresh


unsider

Recommended Posts

Is this method similar to <select> forms?

 

I'm mainly asking because I've never worked with checkboxes, and I googled some articles, but couldn't find exactly what I was looking for.

 

So I'm trying to retain 1 checkbox value that has been checked after a refresh. True or False.

 

If it's similar to <select> here's some code to make it easier on you.

 

<?php
$selected = array();
$cat_values = 20;
for ($i = 0; $i < $cat_values; $i++) {
if ($i == $_POST['cat'])
	$selected[$i] = 'selected';
else
	$selected[$i] = '';
}
?>
<strong>Category</strong><br />
<select name="cat" size="1">
<option value="0"<?php echo $selected[0]; ?>></option>
<option value="1"<?php echo $selected[1]; ?>>test</option>
</select>

 

 

And here's the checkbox.

<input type="checkbox" name="agree" value="agreement">I agree.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/118326-retaining-checkbox-value-after-refresh/
Share on other sites

checkboxes are only sent with the form if selected. that means a simple isset() on the POST element will tell you whether or not you should load it in the checked state. in order to do that, simply specify the attribute checked="true":

 

<input type="checkbox" name="agree" value="agreement"<?php if (isset($_POST['agreement'])) echo ' checked="checked"'; ?>>I agree.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.