PDA

View Full Version : Help with session and multipage forms...


lei
11-29-2005, 04:46 AM
I am working on a two page form and am attempting the use of sessions. I am not that well versed in php and I am having trouble getting the information from the first page of the form to pass to the second page and be submitted along with the rest.

Below is the code for the pages if anyone would be willing to take a look and tell me what i might be doing wrong.....thank you kindly:)

First page php code:


<?php
session_start();
session_register("ABC_Sales");
session_register("ABC_Dispatch");
session_register("SMP_Sales");
session_register("SMP_Dispatch");
session_register("SMP_Receivables");
session_register("SMP_Inventory");
session_register("SMP_Installation");
session_register("Visa");
session_register("MC");
session_register("FedEx");
?>



The session_registers i have are one for each selection or field of the form on both pages of the form... The form itself on this page contains a action to post to the next page

The second page of the form php:


<?php
session_start();
?>


the second page has an action set to send an email through the email script and then redirect to a thank you page on which i have session_destory();

Any help to fix this would be greatly appreciated...

Velox Letum
11-29-2005, 05:02 AM
Sessions are not used to pass form values generally, instead you could use hidden form fields. To answer your question, that is because that's just registering the session variables. To set them use:

$_SESSION['ABC_Sales'] = 'Blah';

lei
11-29-2005, 05:16 AM
thank you very much for replying, but not being very well versed in php, i don't quite understand.

I understand that part about using hidden fields to pass form variables, but how does that work in conjunction with sessions?

Element
11-29-2005, 05:20 AM
Sessions are not used to pass form values generally, instead you could use hidden form fields. To answer your question, that is because that's just registering the session variables. To set them use:

$_SESSION['ABC_Sales'] = 'Blah';

Basically I think he means basically like:



if(isset($_POST['submit'])) {

$form_field_alpha = trim(str_replace(array("$", "?", ":", ";", "=", "/", "*"), '', $_POST['alpha'];));
$form_field_omega = trim(str_replace(array("$", "?", ":", ";", "=", "/", "*"), '', $_POST['omega'];));
// Continue. . .

session_start();
$_SESSION['ALPHA'] = $form_field_alpha;
$_SESSION['OMEGA'] = $form_field_omega;
// Continue. . .

// Continue program. . .

} else {

// Continue submit form, or whatever. . .

}

Just to give you a visual because it sounds like your new. :) :thumbsup:

lei
11-29-2005, 03:38 PM
thank you very much...that helps a bit.....i have another question pertaining to this....is there a way to echo the variables from the previous page and not use a hidden field, so that the person can see what they've already entered but that information is still held and passed along to the final submission?

thank you muchly:)

whizard
11-29-2005, 07:35 PM
Try this:

//Get data from page1 form
$form1_data = $_POST['form1_data'];

//Print it to page
print ("Here is what you said on form1:".$form1_data);

//Save it in a hidden form field so you can pass it along
print("<input type=\"hidden\" name=\"form1_data\" value=\"".$form1_data."\">");


HTH
Dan

lei
11-29-2005, 09:10 PM
thank you so very much..this is exactly what i was looking for...i appreciate you taking the time to respond...:)

whizard
11-29-2005, 10:34 PM
Hey, no problem!

Dan

Element
11-29-2005, 10:39 PM
Just so we don't make post values that don't exist we can do something like:



if(isset($_POST['submit'])) {

// So now we know they were on the last page and hit "submit"
$hidden_form_data = "\n";
if(isset($_POST['form1_data_1'])) {
$hidden_form_data .= "<input type=\"hidden\" name=\"form1_data_1\" value=\"" . $_POST['form1_data_1'] . "\">";
} elseif (isset($_POST['form1_data_2'])) {
$hidden_form_data .= "<input type=\"hidden\" name=\"form1_data_2\" value=\"" . $_POST['form1_data_2'] . "\">";
} // Continue



And of course output is a simpler matter of printing $hidden_form_data wherever you want it.

Velox Letum
11-30-2005, 12:25 AM
Or you can foreach($_POST) to minimize code, and write an if in there to filter out anything you don't want.

if(isset($_POST['submit'])) {
// So now we know they were on the last page and hit "submit"
$hidden_form_data = "\n";

foreach ($_POST as $name => $value) {
if ($name != 'submit') {
$hidden_form_data .= '<input type="hidden" name="' . $name . '" value="' . $value . '">' . "\n";
}
}
} // Continue

Element
11-30-2005, 12:36 AM
Or you can foreach($_POST) to minimize code, and write an if in there to filter out anything you don't want.

if(isset($_POST['submit'])) {
// So now we know they were on the last page and hit "submit"
$hidden_form_data = "\n";

foreach ($_POST as $name => $value) {
if ($name != 'submit') {
$hidden_form_data .= '<input type="hidden" name="' . $name . '" value="' . $value . '">' . "\n";
}
}
} // Continue

Lol. I love how you used "minimize code". Pshaw. And yeah, that works just as great and is more package ready for you, I think. I need to remember the foreach stuff... I like always forget what I can do inside foreach() and just end up finding away around it. (Since I code offline without books or php.net)

lei
11-30-2005, 03:21 PM
thank you so so much...the foreach was just what i needed....perfect..you guys are awesome...:)