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

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.