/    Sign up×
Community /Pin to ProfileBookmark

checkbox into sessions

Ok i have a 3 page form that I am creating for a site and on page2 (aka step2.php) I have 30 checkboxes each representing a type of food. The customer will check all boxes that apply of foods they make at their establishment. I need to find a way to take all checked checkboxes and insert them into a session to be displayed onto “page4” and then able to be changed if needed (go back to page2 to make corrections/changes) and once complete I need to insert all the checked checkboxes into my db. Now I have taken many swings at this with no luck yet. Here is my code.
This is the form…

[code=php]<form method=”POST” enctype=”multipart/form-data” name=”cuisine” action=”step3.php”>
<div align=”left”><h2><font color=”#b80101″>Types of Cuisine</font></h2></div>
<fieldset align=”center”>
<table>
<tr>
<td><input type=”checkbox” name=”food[]” value=”afghan”/></td><td>Afghan</td>
<td><input type=”checkbox” name=”food[]” value=”caribbean”/></td><td>Caribbean</td>
<td><input type=”checkbox” name=”food[]” value=”greek”/></td><td>Greek</td>
<td><input type=”checkbox” name=”food[]” value=”korean”/></td><td>Korean</td>
<td><input type=”checkbox” name=”food[]” value=”pizza”/></td><td>Pizza</td>
<td><input type=”checkbox” name=”food[]” value=”swedish”/></td><td>Swedish</td>

</tr>

<tr>
<td><input type=”checkbox” name=”food[]” value=”american”/></td><td>American</td>
<td><input type=”checkbox” name=”food[]” value=”chinese”/></td><td>Chinese</td>
<td><input type=”checkbox” name=”food[]” value=”indian”/></td><td>Indian</td>
<td><input type=”checkbox” name=”food[]” value=”lebanese”/></td><td>Lebanese</td>
<td><input type=”checkbox” name=”food[]” value=”peruvian”/></td><td>Peruvian</td>
<td><input type=”checkbox” name=”food[]” value=”sweets”/></td><td>Sweets</td>

</tr>

<tr>
<td><input type=”checkbox” name=”food[]” value=”appetizers”/></td><td>Appetizers</td>
<td><input type=”checkbox” name=”food[]” value=”cuban”/></td><td>Cuban</td>
<td><input type=”checkbox” name=”food[]” value=”irish”/></td><td>Irish</td>
<td><input type=”checkbox” name=”food[]” value=”mediterranean”/></td><td>Mediterranean</td>
<td><input type=”checkbox” name=”food[]” value=”portuguese”/></td><td>Portuguese</td>
<td><input type=”checkbox” name=”food[]” value=”thai”/></td><td>Thai</td>

</tr>

<tr>
<td><input type=”checkbox” name=”food[]” value=”brazilian”/></td><td>Brazilian</td>
<td><input type=”checkbox” name=”food[]” value=”french”/></td><td>French</td>
<td><input type=”checkbox” name=”food[]” value=”italian”/></td><td>Italian</td>
<td><input type=”checkbox” name=”food[]” value=”mexican”/></td><td>Mexican</td>
<td><input type=”checkbox” name=”food[]” value=”southwestern”/></td><td>South Western</td>
<td><input type=”checkbox” name=”food[]” value=”turkish”/></td><td>Turkish</td>

</tr>

<tr>
<td><input type=”checkbox” name=”food[]” value=”cajun&creole”/></td><td>Cajun & Creole</td>
<td><input type=”checkbox” name=”food[]” value=”german”/></td><td>German</td>
<td><input type=”checkbox” name=”food[]” value=”japanese”/></td><td>Japanese</td>
<td><input type=”checkbox” name=”food[]” value=”middle_eastern”/></td><td>Middle Eastern</td>
<td><input type=”checkbox” name=”food[]” value=”spanish”/></td><td>Spanish</td>
<td><input type=”checkbox” name=”food[]” value=”vietnamese”/></td><td>Vietnamese</td>

</tr>
</table>
</fieldset>
<a href=”step3.php”><img src=”img/step3.gif” alt=”” border=”0″></a>
</form>[/code]

So first do I need to do name=”food[]” for all check boxes?
second, how do I now go about capturing all checked boxes displying the text for each of them onto another page and then set it up to be inserted into a db?

Any help on this would be greatly appreciated since all attempts I have made have failed.
thanks for all the help.

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@TJ111Dec 10.2007 — When the form is submitted, only checkboxes that are checked are submitted in the query. So you can just loop through each "food[]" and assign it to the session.
[code=php]
session_start()
foreach ($_POST["food[]"] as $val) {
$_SESSION['foods'][] = $val;
}
[/code]

Then $_SESSION['foods'] will be an array containing all the foods.
Copy linkTweet thisAlerts:
@crazy8authorDec 10.2007 — Ok I put that up, then on page4 (to display the info) I put this...
[code=php]<?php if( isset($_SESSION['foods']) ) echo $_SESSION['foods']; ?>[/code]
But I get this error for some reason...
Warning: Invalid argument supplied for foreach() in C:xampphtdocsviewablehsrevisedliveheader.php on line 9[/QUOTE]
any ideas?
Copy linkTweet thisAlerts:
@TJ111Dec 10.2007 — Sorry, there was an error in my syntax. Also, you should do a check to make sure there were foods selected. It should be:
[code=php]
if ($_POST['food']) {
foreach ($_POST["food"] as $val) {
$_SESSION['foods'][] = $val;
}
}
[/code]

This should be at the top of each page, not just page 4, as the POST values will disappear after the next page is submitted.


Then for testing purposes, on page 4 you can have something like:
[code=php]
if ($_SESSION['foods']) {
print_r($_SESSION['foods']);
}
[/code]

That will print information about the variable/array. Then when you want to print all of the foods, use a foreach loop like I did to print each individual food item.
[code=php]
if ($_SESSION['foods']) {
print "<h2>Selected Foods</h2>";
print "<ul>";

foreach($_SESSION['foods'] as $val) {
print "<li>$val</li>";
}
print "</ul>";
}
[/code]
Copy linkTweet thisAlerts:
@crazy8authorDec 10.2007 — Well I dont know if it will matter or not but I am doing an include on all 4 pages to a header...this is my header incase you need to see it or if this would cause problems...

[code=php]<?php session_start(); ?>
<?php
foreach( $_POST as $__key => $__val )
{
$_SESSION['form'][$__key] = $__val;
}
?>
<?php
if ($_POST['food']) {
foreach ($_POST["food"] as $val) {
$_SESSION['foods'][] = $val;
}
}
?>
<?php
session_start();
foreach( $_POST as $__key => $__val )
{
$_SESSION['form3'][$__key] = $__val;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>Twincitieshotspot.com - -Your source for the hottest places in Minnesota-</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link href="css/styles.css" rel="stylesheet" type="text/css">
<link href="css/account.css" rel="stylesheet" type="text/css">
<!--[if IE]>
<link href="ie_styles.css" rel="stylesheet" type="text/css">
<![endif]-->

</head>

<body>
<div id="outer">
<div id="head">

<div id="banner"><a href="index.php"><img src="img/567x100.jpg" alt="" border="0"></a></div>

<div id="search">
<ul>
<li><a href="signup.php">Sign Up</a> |</li>
<li><a href="login.php">Login</a> </li>
</ul>
<table align="right" cellpadding="0" cellspacing="0">
<tr>
<td valign="top"><a href="search.php"><img src="go_but.jpg" alt="" border="0"></a></td>
<td valign="top">
<form action="search.php" method="get" name="search">
<input onclick="this.value='';" name="q" type="text" value="Place, City, Zip, Area Code" style="width:200px">
</form>
</td>
</tr>
</table>
</div>
</div>

<div class="redline">&nbsp;</div>
<?php
$menu = file_get_contents("links_nav.php");
$menu = preg_replace("|<li><a href="" . basename($_SERVER['PHP_SELF']) . "">(.*)</[^>]+></li>|U", "<li class="current">$1</li>", $menu);
echo $menu;
?>[/code]


Thanks for your help
Copy linkTweet thisAlerts:
@TJ111Dec 10.2007 — 1) session_start() has to be called before you can do anything with the $_SESSION superglobal. (edit, just saw your including it at the top, dunno why your calling it a second time?).

2) There's no reason to prepend the variables with "__". The $key and $val variables will not exist outside the foreach statement. From the PHP manual:



PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.

[/quote]




3) Your looping through POST 3 times, each time assigning it two a new second-dimensional array in the $_SESSION superglobal. This is a bit redundant.



Here's how it should be done:

[code=php]
<?php
session_start()
foreach($_POST as $key => $val) {
if ($key == "food") {
$_SESSION['foods'][] = $val;
} else {
$_SESSION['forms'][$key] = $val;
}
}
[/code]
Copy linkTweet thisAlerts:
@crazy8authorDec 10.2007 — Your looping through POST 3 times,[/QUOTE]

i was starting to wonder why some of my other stuff wasnt working. Here is the deal. I have 3 forms, each on their own page. I need to put it all into sessions to be displayed onto a "final" page where all the data from all 3 forms will be displayed, then all the data inserted into my db. So if I have 3 forms how would I go about doing this the right way?

i have included a zip file so you can view it all and so i dont take all sorts of space in here.

thanks for your help

[upl-file uuid=eb81f3ef-e4d9-4f64-966f-ef53a72fcaee size=17kB]forms.zip[/upl-file]
Copy linkTweet thisAlerts:
@TJ111Dec 10.2007 — Use sessions to store the user's location in the forms. Then just forward them accordingly using a process page. So on the link to this form, set it to "process.php". Also, all of the forms should have an action of "process.php".

"process.php":
[code=php]
session_start();
if (!$_SESSION['step']) {
$_SESSION['step'] == 0;
}

switch($_SESSION['step']) {
case 1:
foreach($_POST as $key=>$val) {
$_SESSION[$key] = $val;
}
break;
case 2:
foreach ($_POST['food'] as $val) {
$_SESSION['foods'][] = $val;
}
break;
case 3:
foreach($_POST as $key=>$val) {
$_SESSION['form3'][$key] = $val;
}
break;
}
$_SESSION['step']++;
header("Location:step". $_SESSION['step'] .".php");
[/code]


This is a really simple form script, and I think your making it a little over complicated. Simplicity is key.
Copy linkTweet thisAlerts:
@crazy8authorDec 11.2007 — Ok I have process.php now and have changed the action of my forms to "process.php" and got rid of the session code out of my header file. Now this session stuff is still a bit new to me so I hope im not driving you nuts yet. So now I have just a few questions...

Use sessions to store the user's location in the forms.[/QUOTE]
How exactly do sessions get used to "store a users location"? I have never heard of this before.

So on the link to this form[/QUOTE]
Just so its clear, what link are you refering to, and to what form, considering i technicaly have 3 forms?

Thank you much again for your help. I greatly appreciate it.
Copy linkTweet thisAlerts:
@TJ111Dec 11.2007 — By storing their location in the forms, I mean remembering how many forms they have completed, and forwarding them appropriately. Ie, if they complete form 2, then for some reason browse away from your page, then browse back later (as long as their browser hasn't been closed or the session hasn't expires), they will be redirected to form 3 when they click on the link to the forms.

By "links to the form", I mean any links on your website (menu's, inline, anyplace there's an <a>), point them to process.php instead of "form1.php", "form2.php", etc.

While I'm thinking about it, you could put a link on step4.php that says "Start Over" or something.

step4.php:
[code=html]
<a href="process.php?reset=1">Start Over</a>
[/code]


process.php:
[code=php]
session_start();
if (!$_SESSION['step'] || $_GET['reset'] == 1) {
$_SESSION['step'] = 0; //edit: this was incorrect on the one i posted earlier
//i used $_SESSION['step'] == 0.
}

//then the rest stays the same
[/code]
×

Success!

Help @crazy8 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...