Forum Moderators: coopster
I have a form whose action is a php script which process the form data:
[fixed]
<form method="post" action="script.php">
[/fixed] To this point, I have been using sessions to remember form data so that a user does not lose their inputs after an error is displayed:
[fixed]
$_SESSION['input'] = $_POST['input'];
[/fixed] Then I would check if the session is set, and if, for example, the input data were a select menu, I would check to see the value of the session and if it matches the value then echo 'selected="selected"'.
The difficulty arises with the use of multiple checkboxes that have the same name. Example:
[fixed]
<input type="checkbox" name="example[]" value="1" />apples
<input type="checkbox" name="example[]" value="2" />oranges
[/fixed][fixed]
$_SESSION['example'] = $_POST['example'];
[/fixed] I am using this to check for the session and display checked='checked':
[fixed]
<?php if(isset($_SESSION['example']) && $_SESSION['example'] == 1) echo 'checked="checked"'; ?>
[/fixed] For a value of 2, it would be && session == 2. The problem is when I make the selections (check both boxes) and return a validation error, only the first checkbox is checked. How can I use sessions to retrieve the correct information and check both checkboxes if they where both checked? The form I am working on currently has 19 checkboxes with the same name.
Thank you.
[fixed]
// in script.php
if(isset($_POST['example'])) {
$box = $_POST['example'];
} else {
$box=array();
}
for ($i='0'; $i<count($box); $i++) {
if(!is_numeric($box[$i])) {
$box[$i]='';
}
if(empty($box[$i])) {
unset($box[$i]);}
}
$box = implode ('<>', $box);
$box = '<>'.$box.'';
$_SESSION['example'] = explode('<>',$box[1]);
for ($i='0'; $i<count($box); $i++) {
if(empty($box[$i])) {
unset($box[$i]);
}
}
[/fixed][fixed]
//in form.php
<input type="checkbox" name="example[]" value="1" <?php if(in_array('1', $_SESSION['standardFeatures'])) {echo 'checked="checked"';} ?> />Apples
<input type="checkbox" name="example[]" value="2" <?php if(in_array('2', $_SESSION['standardFeatures'])) {echo 'checked="checked"';} ?> />Oranges
[/fixed] With this method, none of the checkboxes returned checked if a validation error occurred. Because my form does not submit to itself, I had to replace the test variable with something that can be remembered by the server, that being a session. Any suggestions?
Thanks.
[fixed]
$array=$_POST['example'];
$array=explode(" ",$array);
$_SESSION['example'] = $array;
[/fixed] Then to check if the array is set, I print_r() the session on the form. No matter how many checkboxes I check I get this result:
[fixed]
Array ( [0] => Array )
[/fixed] Which means nothing is being passed. My checkboxes look like this:
[fixed]
<input type="checkbox" id="id" name="example[]" value="1" <?php if(isset($_SESSION['example']) && in_array("1", $_SESSION['example'])) echo 'checked="checked"'; ?>
[/fixed] All of them are set to name="example[]". Does anyone see why my script doesn't populate the array with values?
[fixed]
$array=implode(" ",$_POST['example']);
$_SESSION['example'] = explode(" ",$array);
[/fixed] I hope this helps others who are having the same problem. Also, be sure to check if $_POST['example'] is set before creating your variables, array, and setting your session. And, do not forget to properly filter your results later on in the script.
Peace,
Mohamed