Derek
Derek

Reputation: 31

How to remember checkbox input in PHP Forms

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?

Upvotes: 3

Views: 11345

Answers (7)

sachin tendulkar
sachin tendulkar

Reputation: 123

For Example, consider the following code for checkbox :-

<label for="course">Course:</label>
PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />

Then, this would remember the checkbox of "PHP" if it is checked, even if the validation for the page fails and so on for "n" number of checkboxes as shown below:-

<label for="course">Course:</label>
        PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />
        HTML<input type="checkbox" name="course[]" id="course" <?php if     ((!empty($_POST["course"]) && in_array("HTML", $_POST["course"]))) {
echo "checked";
} ?> value="HTML" />
        CSS<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"])     && in_array("CSS", $_POST["course"]))) {
echo "checked";
} ?> value="CSS" />
        Javascript<input type="checkbox" name="course[]" id="course" <?php if     ((!empty($_POST["course"]) && in_array("Javascript", $_POST["course"]))) {
echo "checked";
} ?> value="Javascript" />

And most importantly, do not forget to declare the "course" variable as an array at the start of the code as shown below :-

$course = array();

Upvotes: 3

user1914424
user1914424

Reputation: 11

My array has name="radioselection" and value="1", value="2", and value="3" respectively and is a radio button array... how to I check if the radio value is selected using this code I tried:

<?php echo (isset($_POST['radioselection']) == '1'?'checked="checked"':'') ?> />

Upvotes: 0

Uchy
Uchy

Reputation: 11

I have been battling how to create sticky check box (that is able to remember checked items any time you visit the page). Originally, I get my values from a database table. This means that my check box value is entered to a column on my db table.

I created the following code and it works just fine. I did not want to go through that whole css and deep coding, so...

CODE IN PHP

$arrival = ""; //focus here.. down
if($row['new_arrival']==1) /*new_arrival is the name of a column on my table that keeps the value of check box*/
{$arrival="checked";}// $arrival is a variable
else
{$arrival="";};
echo $arrival;

<b><label for ="checkbox">New Arrival</label></b>&nbsp;&nbsp;&nbsp;
<input type="checkbox" name ="$new_arrival" value="on" '.$arrival.' /> &nbsp;(Tick box if product is new) <BR><BR>

Upvotes: 1

Randell
Randell

Reputation: 6170

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

Upvotes: 0

Asaph
Asaph

Reputation: 162771

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.

Upvotes: 0

St. John Johnson
St. John Johnson

Reputation: 6660

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.

Upvotes: 5

p.g.l.hall
p.g.l.hall

Reputation: 1961

<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']);

Upvotes: 0

Related Questions