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...
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.
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.
PHP Code:
session_start()
foreach ($_POST["food[]"] as $val) {
$_SESSION['foods'][] = $val;
}
Then $_SESSION['foods'] will be an array containing all the foods.
Sorry, there was an error in my syntax. Also, you should do a check to make sure there were foods selected. It should be:
PHP Code:
if ($_POST['food']) {
foreach ($_POST["food"] as $val) {
$_SESSION['foods'][] = $val;
}
}
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:
PHP Code:
if ($_SESSION['foods']) {
print_r($_SESSION['foods']);
}
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.
PHP Code:
if ($_SESSION['foods']) {
print "<h2>Selected Foods</h2>";
print "<ul>";
foreach($_SESSION['foods'] as $val) {
print "<li>$val</li>";
}
print "</ul>";
}
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...
PHP Code:
<?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">
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.
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.
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.
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":
PHP Code:
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");
This is a really simple form script, and I think your making it a little over complicated. Simplicity is key.
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.
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
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.
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:
HTML Code:
<a href="process.php?reset=1">Start Over</a>
process.php:
PHP Code:
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.
}
Bookmarks