Jump to content

[SOLVED] saving checkbox array in a $_SESSION


denoteone

Recommended Posts

I have a form that has check boxes, on submit I would like to save the check box array in a session variable.

 

<form action="<?=$PHP_SELF?>" method="post">
<h5>Support Services</h5>
<p>Customer wants the following service (choose all that apply)<br/>
<INPUT TYPE=CHECKBOX NAME="servicelevel[]" value="Support Desk"> 1-800 Support Desk<br/>
<INPUT TYPE=CHECKBOX NAME="servicelevel[]" value="Email Support">E-Mail Support<br/>
<INPUT TYPE=CHECKBOX NAME="servicelevel[]" value="Remote Tech Support">Remote Technical Support<br/>
<INPUT TYPE=CHECKBOX NAME="servicelevel[]" value="Depot Replacement">Depot Replacement<br/>
<INPUT TYPE=CHECKBOX NAME="servicelevel[]" value="On Site Service">On-Site Service</p><br/>
<input type="submit" name="submit" value="Next">
</form>

this is what I have in my php

$_SESSION['servcie_level'] =  $_POST['servicelevel'];

 

and then I would like to later show the value of  $_SESSION['servcie_level']  so I have this on another page.

 

Customer Wants the Following Services:        <?
	foreach ( $_SESSION['service_level']  AS $name => $value )
			{
		echo $value . "<br />";
			} ?>

 

but I am still getting an error. when it try's to show it on the screen.  any ideas?

PHP will not recognize the form name servicelevel[] as an array, but rather as a string. When you run the foreach loop, PHP requires an array, but finds a string instead.

 

To trouble shoot this issue print the form input:

print_r ($_POST['servicelevel']);

 

Hope this helps!

Woops, never mind, I wasn't initializing the variable like you probably have!

 

You can avoid the error (although in this case it was useful for debugging) by wrapping your foreach in an if statement:

 

<?php
if (is_array($SESSION['service_level'])) {
foreach ( $_SESSION['service_level']  AS $name => $value ) {
	echo $value . "<br />";
}
}
?>

I'm not sure if that's your error, here's something I noticed:

 

<form action="<?=$PHP_SELF?>" method="post">

 

will produce:

 

<form action="" method="post">

 

Try:

 

<form action="<?php echo $PHP_SELF; ?>" method="post">

<?=$varname?> is shorthand of the <?php echo $varname; ?>

Archived

This topic is now archived and is closed to further replies.

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