Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
steveirmy
1,614 PointsMultipage form
Hi Guys,
I did the contactform php course and i managed to build a working form!
In my own project i am building a 2 page contract form.
How do i make sure content of 2 pages is mailed after submitting? How do i do this?
Roy
13 Answers
Randy Hoyt
Treehouse Guest TeacherThink about it step by step in terms of requests and responses, and you'll see the challenge with multipage forms:
- Step 1. REQUEST: A site visitor requests the page.
- Step 2. RESPONSE: The server sends back page 1 of the form blank.
- Step 3. REQUEST: The visitor fills out the form and submits the data for page 1 with another request.
- Step 4. RESPONSE: The server receives the data with the request and sends back page 2 of the form blank.
When server responds back to the browser loads page 2, the server then loses all memory of the request in step 3!
There are a few ways to handle this.
- The server could send an email in Step 4.
- The server could write the values from the form fields to a database in Step 4.
- The server in Step 4 could send the browser back the values from the fields it received in Step 3.
The easiest way would probably be that third option:
- Step 4 (revised): RESPONSE: The server receives the data with the request and sends back page 2 of the form blank and hidden fields with all the values from page 1.
- Step 5: REQUEST: The visitor fills out the form and submits the data for page 2 and the hidden values from page 1 with another request.
- Step 6: RESPONSE: The server sends the email with all the values from both pages and redirects to a Thank You page.
The one problem with this approach is that you won't receive any data until page 2 is completed. Depending on the context, that may be what you want.
Does that make sense?
steveirmy
1,614 PointsYes absolutely! thanks! But how do I input the values of the first page on the second page? <input type="hidden" name="first_name" value="....">
what should i put in the value position?
How about with checkboxes and other input methods?
Roy
Randy Hoyt
Treehouse Guest TeacherWhen you receive the first form submission, you'll have the values in the $_POST array:
- $_POST["name"]
- $_POST["address"]
- and so on
When you generate the form, you'll need to use those in the fomr
<?php
// controller code
$page_one_name = $_POST["name"];
$page_one_name = $_POST["address"];
?>
...
<form>
<!-- page two fields -->
<input name="name" value="<?php echo htmlspecialchars($page_one_name); ?>">
<input name="address" value="<?php echo htmlspecialchars($page_one_address); ?>">
</form>
Does that help?
steveirmy
1,614 PointsThnx! getting close now!
Somehow it doesnt display the text from the formfields i have. so users can enter a text.. this text is somehow not displayed?
Checkbox items work perfectly!
Also..
I have the total price of the selection displayed as <span id="totaalprijs" name="sumprice"> on the first page of the form. How can i display this one on the second page?
Roy
Randy Hoyt
Treehouse Guest TeacherYou'll have to include some code or something for me to figure out what might be wrong.
When you receive the page 1 form submission, are you receiving the total price? You may have to add that as a hidden field on the page 1 form so that you can pass it to the server so the server can send it back to page 2.
steveirmy
1,614 Pointsthis is the code for the end of the form in page1:
<div id="totalprice">
<hr class="total">
<p class="totalprice">Totaal: <h7>€<span id="totaalprijs" name="sumprice">20</span></span></h7><br>/maand</p><br>
<a href="index.php" id="cancelorder" onclick="if (!confirm('Weet u zeker dat u uw bestelling wilt annuleren?')) return false;">Annuleren</a>
<input type="submit" name="continue" id="submit" value="Ik ben er klaar voor, let's go!"></a><br>
<input type="hidden" name="sumprice" value="">
</div>
</form>
These are the hidden fields for page2:
<form name="myForm" action="payment.php" onSubmit="return validateForm();" method="post">
<input type="hidden" name="domainreg" value="<?php echo htmlspecialchars($domainreg); ?>">
<input type="hidden" name="cars" value="<?php echo htmlspecialchars($cars); ?>">
<input type="hidden" name="domainnew" value="<?php echo htmlspecialchars($domainnew); ?>">
<input type="hidden" name="domainmove" value="<?php echo htmlspecialchars($domainmove); ?>">
<input type="hidden" name="domainname" value="<?php echo htmlspecialchars($domainname); ?>">
<input type="hidden" name="hosting" value="<?php echo htmlspecialchars($hosting); ?>">
<input type="hidden" name="exchange" value="<?php echo htmlspecialchars($exchange); ?>">
<input type="hidden" name="domainnameexchange" value="<?php echo htmlspecialchars($domainnameexchange); ?>">
<input type="hidden" name="antivirus" value="<?php echo htmlspecialchars($antivirus); ?>">
<input type="hidden" name="backup" value="<?php echo htmlspecialchars($backup); ?>">
<input type="hidden" name="office" value="<?php echo htmlspecialchars($office); ?>">
<input type="hidden" name="desktop" value="<?php echo htmlspecialchars($desktop); ?>">
<input type="hidden" name="sugar" value="<?php echo htmlspecialchars($sugar); ?>">
<input type="hidden" name="noma" value="<?php echo htmlspecialchars($noma); ?>">
<input type="hidden" name="sumprice" value="<?php echo htmlspecialchars($sumprice); ?>">
Hope this helps
Roy
Randy Hoyt
Treehouse Guest TeacherAnd which fields are having the issues? Describe (a) what you expect to happen and (b) what is happening instead of that.
steveirmy
1,614 Points<input type="hidden" name="sumprice" value="<?php echo htmlspecialchars($sumprice); ?>">)
is not displaying the total price: <span id="totaalprijs" name="sumprice"> (the output of this code = sumprice)
Also:
<input type="hidden" name="exchange" value="<?php echo htmlspecialchars($exchange); ?>">)
should output some text. The field that comes with this item is a textfield. Somehow, the text entered here is not displayed.
Roy
Randy Hoyt
Treehouse Guest TeacherThese are hidden fields, so they would not be displayed on the page. You use them to store the values so that they will get submitted to the server on the next form submission.
Does that make sense?
steveirmy
1,614 PointsYes, but thats the whole idea. I dont see the values of them after submission. All values work exept there two.
How come?
Randy Hoyt
Treehouse Guest TeacherAre you saying that the other fields work correctly? $domainreg, $backup, $sugar, etc. are all working correctly? If so, can you post the code from page2 above the form where you load the $_POST values into the $sumprice variable and the $exchange variable?
steveirmy
1,614 PointsBTW. all textfields field types are not loading.. so also domainname and domainnameexchange. sumprice is also important
$domainreg = $_POST["domainreg"]; $cars = $_POST["cars"]; $domainnew = $_POST["domainnew"]; $domainmove = $_POST["domainmove"]; $domainname = $_POST["domainname"]; $hosting = $_POST["hosting"]; $exchange = $_POST["exchange"]; $domainnameexchange = $_POST["domainnameexchange"]; $antivirus = $_POST["antivirus"]; $backup = $_POST["backup"]; $office = $_POST["office"]; $desktop = $_POST["desktop"]; $sugar = $_POST["sugar"]; $noma = $_POST["noma"]; $sumprice = $_POST["sumprice"];
Thank you so much for your help!
Randy Hoyt
Treehouse Guest TeacherCan you include the code from page1 that creates the fields in the form? The snippet you provided doesn't shows any fields. Perhaps it would help if you posted the two complete PHP files somewhere that I could download them?
steveirmy
1,614 PointsDownload link: http://we.tl/SK7w5qfUKi
i want the sumprice to show up also in the payment page (second page) inside the submit button in the bottom. I want this price to be sent to paypal.
Thank you for this personal approach!
steveirmy
1,614 Pointscan you see the code? somehow it is not showing after submitting.
Randy Hoyt
Treehouse Guest TeacherFirst of all, regarding the input fields ... you have a number of text fields like this:
<input type="text" size="20">
Without a name, there's no way for the server to access the value:
<input type="text" size="20" name="something">
Does that help?
Randy Hoyt
Treehouse Guest TeacherSecond of all, regarding $sumprice ... You have this hidden field:
<input type="hidden" name="sumprice" value="">
That's the value that gets sent to the server. It looks like page1 will always send a blank value right now. Whatever code you have written that populates the span (is that JavaScript that does that?), it will need to populate the value in this hidden input field.
Does that help?
steveirmy
1,614 Pointsnow i have this:
Yes its javascript. the code is in the bottom. so now i have this: <input type="hidden" name="sumprice" value="<?php echo htmlspecialchars($sumprice); ?>">
so it should be like this?
<input type="hidden" name="sumprice" value="<?php echo htmlspecialchars(<span id="totaalprijs" name="sumprice">); ?>">
Should i also change: $sumprice = $_POST["sumprice"];??
Randy Hoyt
Treehouse Guest TeacherNo, not like that. You need to change the JavaScript. Line 375 has this:
document.getElementById('totaalprijs').innerHTML = total;
That loads the total into the span. You need a similar line like that to also load the total into the hidden field.
steveirmy
1,614 PointsI guess i cant simply copy that line right? So include that line in value?
Randy Hoyt
Treehouse Guest TeacherChange your hidden input field from this ...
<input type="hidden" name="sumprice" value="">
... to this ...
<input type="hidden" name="sumprice" value="" id="sumprice">
Then add this in a new line 376:
document.getElementById('sumprice').value = total;
The first change adds an id to the hidden input field of sumprice. The JavaScript code finds an element with an id of sumprice and loads the total into its value.
Does that help?
steveirmy
1,614 PointsNope, still not working..
so i need to add the javascript only to page one?
I now have a inputfield like this:
<input type="hidden" name="sumprice" value="" id="sumprice">
Randy Hoyt
Treehouse Guest TeacherI spent quite a bit of time looking at the code today, and it seems like too many files are missing and features not working for me to figure out what's going on. Are there more PHP files than just these two?
Elliott Frazier
Courses Plus Student 9,647 Pointsjust looking at this discussion makes my head hurt! lol :P
Elliott Frazier
Courses Plus Student 9,647 PointsElliott Frazier
Courses Plus Student 9,647 Pointstagging Randy Hoyt