davidcriniti Posted March 21, 2011 Share Posted March 21, 2011 Hi, I've tried to follow a couple tuts on multi-page forms using php. The reason I'm doing it is that I'd like to create a mock-job application form for my students at school. This would be too long to have on one page. Anyway, I've created the most simple form I could think of and was hoping someone could help me out and show me how to get the info from page 1 to 2, then the info from pages 1 and 2 to 3, and then submit the info from pages 1, 2 and 3 to the database. Page 1 asks for firstname, page 2 asks for lastname, and page 3 asks for email address. The fourth page is the one that processes the form. Page 1: (app_form_1.php) <form id="form1" name="form1" method="post" action="app_form_2"> Firstname: <label> <input type="text" name="firstname" id="firstname" /> </label> </form> <p> <label> <input type="submit" name="Continue" id="Continue" value="Continue" /> </label> Page 2 (app_form_2.php) <form id="form1" name="form1" method="post" action="app_form_3"> Lastname: <label> <input type="text" name="firstname" id="firstname" /> </label> </form> <p> <label> <input type="submit" name="Continue" id="Continue" value="Continue" /> </label> Page3 ( app_form_3.php ) <form id="form1" name="form1" method="post" action="app_form_process"> Email address: <label> <input type="text" name="emailaddress" id="emailaddress" /> </label> </form> <p> <label> <input type="submit" name="submit" id="Submit" value="Submit" /> </label> Page 4 (app_form_process.php ) <?php global $_POST; $firstname = $_POST["firstname"] ; $lastname = $_POST["lastname"]; $emailaddress = $_POST["emailaddress"]; //**********************SEND TO DATABASE**************************** //MySQL Database Connect include 'mysql_connect.php'; $query = "INSERT INTO application_form (firstname, lastname, emailaddress)" . "VALUES ('$firstname', '$lastname', '$emailaddress')"; //if($query){echo 'data has been placed'} mysql_query($query) or die(mysql_error()); ?> Any help is much appreciated. Thanks, Dave Quote Link to comment Share on other sites More sharing options...
xangelo Posted March 21, 2011 Share Posted March 21, 2011 The easiest way is use PHP's sessions to store all the information between pages. You could leave app_form_1.php without the session code since it doesn't need to maintain any information. On submit (ensure the action is app_form_2.php, not just app_form_2) it will send the details of your form to app_form_2.php This is what the start of app_form_2.php could look like. Make sure the session_start(); method appears at the VERY top of your page. <?php session_start(); $_SESSION['firstname'] = $_POST['firstname']; ?> <form id="form1" name="form1" method="post" action="app_form_3.php"> Lastname: <label> <input type="text" name="firstname" id="firstname" /> </label> </form> <p> <label> <input type="submit" name="Continue" id="Continue" value="Continue" /> </label> In this way, app_form_3.php would look like this: <?php session_start(); /* * To change this template, choose Tools | Templates * and open the template in the editor. */ $_SESSION['lastname'] = $_POST['lastname']; ?> <form id="form1" name="form1" method="post" action="app_form_process.php"> Email address: <label> <input type="text" name="emailaddress" id="emailaddress" /> </label> </form> <p> <label> <input type="submit" name="submit" id="Submit" value="Submit" /> </label> Your app_form_process.php only needs to be modified slightly: <?php session_start(); $firstname = $_SESSION['firstname'] ; $lastname = $_SESSION['lastname']; $emailaddress = $_POST["emailaddress"]; Let me know if you run into any other problems. Quote Link to comment Share on other sites More sharing options...
duster889 Posted March 22, 2011 Share Posted March 22, 2011 Using sessions is one way as stated above but you might want to use the "field_forward" function as it is much easier to use: http://www.zend.com//code/codex.php?ozid=1092&single=1 The function will gather all variables from previous page POSTs and put them into hidden input variables on the new page. You just call the function on each page. This chains all the pages together and is very simple to use. <form> <?php require('fieldforwarder.php'); echo field_forwarder(); ?> </form> Quote Link to comment Share on other sites More sharing options...
davidcriniti Posted March 22, 2011 Author Share Posted March 22, 2011 Thanks for those replies. I got the result I wanted with xangelo's advice, but will also have a look at your way too duster889. I notice I made a few silly mistakes in the code I provided above, so I might post the working code here for the benefit of anyone who looks up this problem in the future: Code from the first page ( app_form_1.php ) : <form id="form1" name="form1" method="post" action="app_form_2.php"> Firstname: <label> <input type="text" name="firstname" id="firstname" /> </label> <p> <label> <input type="submit" name="submit" id="Go to page 2" value="Go to page 2" /> </label> </form> Code from the second page ( app_form_2.php ): <?php session_start(); $_SESSION['firstname'] = $_POST['firstname']; ?> /* * HTML code - DOCTYPE etc */ <form id="form1" name="form1" method="post" action="app_form_3.php"> Lastname: <label> <input type="text" name="lastname" id="lastname" /> </label> <p> <label> <input type="submit" name="submit" id="Go to page 3" value="Go to page 3" /> </label> </form> Code from the third page (app_form_3.php ) : <?php session_start(); $_SESSION['lastname'] = $_POST['lastname']; ?> /* * HTML code - DOCTYPE etc */ <form id="form1" name="form1" method="post" action="app_form_process.php"> Email address: <label> <input type="text" name="emailaddress" id="emailaddress" /> </label> <p> <label> <input type="submit" name="submit" id="Submit" value="Submit" /> </label> </form> Code from the 4th page ( app_form_process.php ): <?php session_start(); $firstname = $_SESSION['firstname'] ; $lastname = $_SESSION['lastname']; $emailaddress = $_POST["emailaddress"]; //**********************SEND TO DATABASE**************************** //MySQL Database Connect include 'mysql_connect.php'; $query = "INSERT INTO application_form (firstname, lastname, emailaddress)" . "VALUES ('$firstname', '$lastname', '$emailaddress')"; //if($query){echo 'data has been placed'} mysql_query($query) or die(mysql_error()); ?> Quote Link to comment Share on other sites More sharing options...
xangelo Posted March 22, 2011 Share Posted March 22, 2011 Glad to help. One thing I would suggest, however, is to sanitize your input variables before you insert them into your database. http://php.net/manual/en/function.mysql-real-escape-string.php Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.