Forum Moderators: coopster

Message Too Old, No Replies

Passing Data php

pass data from form 1 to 2 then 3 in php

         

Msiko

11:17 am on May 15, 2009 (gmt 0)

10+ Year Member



Hi everybody.

Can someone show me the way to pass data from form_1 then to form_2 then to form_3 in php.

I can get the ['var'] in form 2 but when i call the action="" to form_3 the form is blank, no ['var'].

THE THIRD FORM MUST BE THE ONE SUBMITTING TO THE SPECIFIC EMAIL. When I get the email all the ['var'] are blank.

I am doing this without a database so that my forms can be self procees.

Can anyone help this up and comming php GURU fro SA.

Thanks in advance.

penders

11:34 am on May 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I can get the ['var'] in form 2 but when i call the action="" to form_3 the form is blank, no ['var'].

When form 1 is submitted you can store the necessary 'persistent' vars in form 2 (when you write out form 2) in hidden fields, like:

<input type="hidden" name="form1_someinfo" value="ThisValueCameFromForm1">

When form 2 is submitted then this hidden field is submitted as well - you then have to read this hidden field when form 2 is processed, otherwise the information will be lost.

Is that the sort of thing you are doing?

Or, you could use SESSIONs [uk2.php.net], which would avoid having to pass information from form to form.

Welcome to WebmasterWorld :)

Msiko

2:12 pm on May 15, 2009 (gmt 0)

10+ Year Member



Thanks for the quick and straight to the point reply.

What must I put in value="ThisValueCameFromForm1"?

I can echo the 'var' in form2, But I cant pass them to the next form. Take a look on the code I am ussing:

Form1
<input name="name" type="text" id="name" size="30" />

Form2
//here I call the hidden 'var' from form1 by this code:

<input name="name" type="hidden" id="name" size="30" /><?php $name = $_POST['name']; echo $name; ?>

Form3
// here I receive blank Name constant without a 'var' that appeared in form2, with this code

<input name="name" type="hidden" id="name" size="30" />
<?php $name = $_POST['name']; echo $name; ?>

Hope i makes sense.

penders

3:10 pm on May 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



//here I call the hidden 'var' from form1 by this code:
<input name="name" type="hidden" id="name" size="30" /><?php $name = $_POST['name']; echo $name; ?>

I think should read:

<input name="name" type="hidden" id="name" value="<?php echo $_POST['name']; ?>" />

The value of the INPUT element comes from the value of the VALUE attribute, not from what follows it. What follows the INPUT element will get output to the page, which is not what you require in this instance.

idfer

4:04 pm on May 15, 2009 (gmt 0)

10+ Year Member



Don't forget to pass all data, especially user input, through htmlentities() before outputing. This way if a visitor enters a < either accidently or on purpose, it won't break your 2nd and 3rd forms. So:

<input name="name" type="hidden" id="name" value="<?php echo htmlentities($_POST['name']); ?>" />

Msiko

7:10 pm on May 18, 2009 (gmt 0)

10+ Year Member



Thanks a lot guys for your efforts and input to bennefit this junkee. But I still cant get it to work on form3.

The name constant still displays an empty var.

Hew!

penders

8:36 pm on May 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



But I still cant get it to work on form3.

Did the values get to form2? If you view the source of your 2nd page do you see the values from form1? It's the same principle to then get them to form3.

An example...

With 4 pages. "page1.php", "page2.php" and "page3.php" each have a form continuing from the last so the values are passed along. "the_end.php" just displays all the values.

page1.php (Form 1)

Gets the 'Name' from the user.
<form action="page2.php" method="post"> 
Name: <input type="text" name="f1name" value="">
<input type="submit" name="f1submit" value="Next">
</form>

page2.php (Form 2)

Stores the 'Name' from Form 1 and gets the 'Address' from the user.
<form action="page3.php" method="post"> 
<input type="hidden" name="f2name" value="<?php echo htmlentities($_POST['f1name']); ?>">
Address: <input type="text" name="f2address" value="">
<input type="submit" name="f2submit" value="Next">
</form>

page3.php (Form 3)

Stores the 'Name' and 'Address' from Form 2 and gets the 'Phone' from the user.
<form action="the_end.php" method="post"> 
<input type="hidden" name="f3name" value="<?php echo htmlentities($_POST['f2name']); ?>">
<input type="hidden" name="f3address" value="<?php echo htmlentities($_POST['f2address']); ?>">
Phone: <input type="text" name="f3phone" value="">
<input type="submit" name="f3submit" value="Send">
</form>

the_end.php

Retrieves the 'Name', 'Address' and 'Phone' from Form 3 and does something.
<p>You entered:<br> 
Name: <?php echo htmlentities($_POST['f3name']); ?><br>
Address: <?php echo htmlentities($_POST['f3address']); ?><br>
Phone: <?php echo htmlentities($_POST['f3phone']); ?>
</p>

Form 3 basically ends up with ALL the values from Form 1 and Form 2 but in hidden fields. Does that work for you?

Msiko

5:39 am on May 19, 2009 (gmt 0)

10+ Year Member



Thanks a lot buddy penders, you are a life saver. I will get back to you shortly if I encounter problems. Thanks.

Msiko

5:48 am on May 19, 2009 (gmt 0)

10+ Year Member



Thanks a lot buddy penders you are a real life saver. i am designing an online booking form where by a user will select a travel destination and the next form must echo the total amount and all the entered details in the previous form. But I will let you know if I get stuck along the way.
Thanks again.

Msiko

2:51 pm on May 19, 2009 (gmt 0)

10+ Year Member



Hi I am back again.
Todays Problem:
//Lets say I have 5 Cities on menu
//I also have up to 7 of passengers in the drop down menu
// each city has its own price
//Total Price is determined by the no of passengers and City chosen by the user.
// eg. if no of passenger <= 4 then total is A
but if no of passengers > 4 then total is B
Also note that each and every city has its unique price.

Here's what i have done: if($city == "Witbank" && $passengers == 3) {$total = $witbank1 * $witbank2;}else {$total = $witbank1 * $passengers;}

//when I tell my script that i have chosen another city it does all the calculations but the total is wrong. How do I get this right

Thanks in advance.