views:

264

answers:

4

For usability purposes I like to set up my form fields this way:

<?php

$username = $_POST['username'];
$message  = $_POST['message'];

?>

<input type="text" name="username" value="<?php echo $username; ?>" />

<textarea name="message"><?php echo $message; ?></textarea>

This way if the user fails validation, the form input he entered previously will still be there and there would be no need to start from scratch.

My problem is I can't seem to keep check boxes selected with the option that the user had chosen before (when the page refreshes after validation fails). How to do this?

A: 
<input type="checkbox" name="somevar" value="1" <?php echo $somevar ? 'checked="checked"' : ''; ?>/>

Also, please consider sanitising your inputs, so instead of:

$somevar = $_POST['somevar'];

...it is better to use:

$somevar = htmlspecialchars($_POST['somevar']);
p.g.l.hall
I have to ask why so many downvotes for this. It's a good answer, but just missing explanation and code tags. (use four spaces or `s to start code syntax)
St. John Johnson
I think it's because I had to edit it a load of times. I just bashed something out quickly without previewing it and had to correct it about 3 times. >_<
p.g.l.hall
+1 for mentioning sanitizing the input.
Duncan
Why sanitizing when you're just checking if the value exists? No need.
Terw
+4  A: 

My first suggestion would be to use some client-side validation first. Maybe an AJAX call that performs the validation checks before continuing.

If that is not an option, then try this:

<input type="checkbox" name="subscribe" <?php echo (isset($_POST['subscribe'])?'checked="checked"':'') ?> />

So if subscribe is = 1, then it should select the box for you.

St. John Johnson
It's checked="checked" not selected
Terw
Fixed, sorry. I was thinking <option> tags.
St. John Johnson
This will produce an array index undefined warning if the checkbox is unchecked.
Asaph
@Asaph, good point, I changed it to isset. If the value is sent via POST, it HAS to be 1 (meaning checked). Therefore, we can just check to see if the key exists.
St. John Johnson
@Johnson You know that you can use the value="" attribute with the checkbox too? So it doesn't have to be 1, but no matter what the value is, and if it's checked, then it's at least set . So the isset() is a good way to go
Terw
A: 

When the browser submits a form with a checked checkbox, it sends a variable with the name from the name attribute and a value from the value attribute. If the checkbox is not checked, the browser submits nothing for the checkbox. On the server side, you can handle this situation with array_key_exists(). For example:

<?php
$checkedText = array_key_exists('myCheckbox', $_POST) ? ' checked="checked"' : '';
?>
<input type="checkbox" name="myCheckbox" value="1"<?php echo $checkedText; ?> />

Using array_key_exist() avoids a potential array index undefined warning that would be issued if one tried to access $_POST['myCheckbox'] and it didn't exist.

Asaph
You can use **isset** instead, it's faster as it is a language construct and not a function. The only case where you would need **array_key_exist** instead of **isset** is if the value of the array at that key was "null" This will never happen in POST/checkbox data.
St. John Johnson
A: 

You may add this to your form:

<input type="checkbox" name="mycheckbox" <?php echo isset($_POST['mycheckbox']) ? "checked='checked'" : "" ?> />

isset checks if a variable is set and is not null. So in this code, checked will be added to your checkbox only if the corresponding $_POST variable has a value..

Randell
-1 identical answer as mine. And **checked** is not XHTML valid. It needs to be **checked="checked"**
St. John Johnson
Corrected `checked` for xhtml compliance. Disclaimer: I didn't bother with xhtml compliance in the first place since the answer is intended to answer the question directly. Identical would mean you explained what `isset` does, which **you didn't**.
Randell