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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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 />";
}
}
?>

Link to comment
Share on other sites

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; ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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