Forum Moderators: coopster

Message Too Old, No Replies

Sticky forms - Multiple Checkboxes

Extensive searches yield nothing of use

         

max4

9:20 pm on Jun 5, 2009 (gmt 0)

10+ Year Member



Hello,

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.

Gibble

9:28 pm on Jun 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This could help
[webmasterworld.com...]

$_POST['example'] should contain an array

max4

9:36 pm on Jun 5, 2009 (gmt 0)

10+ Year Member



Thank you Gibble; I'm looking at the link now.

max4

10:20 pm on Jun 5, 2009 (gmt 0)

10+ Year Member



From your link I've come up with the following:
[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.

max4

3:18 am on Jun 8, 2009 (gmt 0)

10+ Year Member



Okay, I've been working with this for a while now; and I've come up with the following:
[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?

max4

6:36 pm on Jun 8, 2009 (gmt 0)

10+ Year Member



I just solved the problem by first imploding to format the array and then exploding to create the session in such a way that use of the in_array() function is plausible. Also, I changed my logic slightly. This is the working version:
[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