babymac Posted May 18, 2013 Share Posted May 18, 2013 Hi everyone, I'm new to PHP and am taking a class but things aren't coming together as I'd hoped. I'm trying to utilize sessions to carry info from page to page. It brings it to the next page, but not the next. What I'm trying to do is: -Make a simple input form. -Show entered information on next page via the use of a session. -Third page shows final information. On a bigger scale, I'd like to have some if statements added if a field is left blank but want to get this figured out first. Thanks for any comments/suggestions! -Lórien FIRST PAGE: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://wwww.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title> Input Form </title></head><body> <form action="pa2.php" method="POST"> <p>First Name: <input type='text' name='fname' size='30'/> </p> <p>Last Name: <input type='text' name='lname' size='30'/> </p> <p>Email Address: <input type='text' name='email' size='45'/> </p> <input type="submit" name='enter' value="Submit" /><br/> </form> </body></html> SECOND PAGE: <?php //Create a script that starts a session: session_start(); $_SESSION['fname'] = $_POST['fname']; $_SESSION['lname'] = $_POST['lname']; $_SESSION['email'] = $_POST['email']; // Print the received data: echo '<p> You entered your first name as: ' . $_SESSION['fname'] . ' </p> <p> You entered your last name as: ' . $_SESSION['lname'] . ' </p> <p> You entered your email as: ' . $_SESSION['email'] . ' </p>'; ?> <a href="http://studentweb.wilmu.edu/sites/lpeer79627/pa2.html"><br />Click to go back and change your information</a> <a href="http://studentweb.wilmu.edu/sites/lpeer79627/pa2final.php"><br />Yes, my information is correct!</a> THIRD PAGE: <?php //Create a script that starts a session: session_start(); $_SESSION['fname'] = $_POST['fname']; $_SESSION['lname'] = $_POST['lname']; $_SESSION['email'] = $_POST['email']; // Print the received data: print '<p><h2>Here is your information:</h2></p> <p> Name: ' . $_SESSION['fname'] . ', ' . $_SESSION['lname'] . ' </p> <p> Email: ' . $_SESSION['email'] . ' </p>'; ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 18, 2013 Share Posted May 18, 2013 You're over writing the values on the third page with presumably non existent posted values. Quote Link to comment Share on other sites More sharing options...
babymac Posted May 18, 2013 Author Share Posted May 18, 2013 Yes! I just figured it out. Thank you so so much! For future views to this post, I needed to delete the session variables at the top of the third page and just echo in the statements. I do have one other question - I have updated the code a bit and would like to have one link show to go back and edit info, and another link show to continue to final screen. They both appear at the bottom, and I don't know how to include them inside the php code. Thoughts? <?php //Create a script that starts a session: session_start(); $_SESSION['fname'] = $_POST['fname']; // First Name $_SESSION['lname'] = $_POST['lname']; // Last Name $_SESSION['email'] = $_POST['email']; // Email Address // Check if the form has been submitted: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form: if ( (empty($_POST['fname'])) or (empty($_POST['lname'])) or (empty($_POST['email'])) ) // Forgot a field: echo '<p>Please make sure all input fields are entered.</p>'; } if ( (!empty($_POST['fname'])) && (!empty($_POST['lname'])) && (!empty($_POST['email'])) ) { // Print the received data: echo '<p> You entered your first name as: ' . $_SESSION['fname'] . ' </p> <p> You entered your last name as: ' . $_SESSION['lname'] . ' </p> <p> You entered your email as: ' . $_SESSION['email'] . ' </p>'; } ?> <a href="http://studentweb.wilmu.edu/sites/lpeer79627/pa2.html"><br />Click to go back and change your information</a> <a href="http://studentweb.wilmu.edu/sites/lpeer79627/pa2final.php"><br />Continue to final screen</a> Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 18, 2013 Share Posted May 18, 2013 Put them where you want them. Quote Link to comment Share on other sites More sharing options...
babymac Posted May 18, 2013 Author Share Posted May 18, 2013 (edited) I'm getting parse errors if I paste them into the php area. Edited May 18, 2013 by babymac Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 18, 2013 Share Posted May 18, 2013 You clearly know how to echo HTML within PHP cause you're doing it. You also know how to end PHP and do straight HTML. What are you confused about? Quote Link to comment Share on other sites More sharing options...
babymac Posted May 18, 2013 Author Share Posted May 18, 2013 Sorry - I guess I'm not sure. I am trying to put the href's at the bottom of the page up into the if statements. One to go back if fields are not entered, and one to move forward to the next page. I've tried wrapping them in '<p></p>' statments, etc, and nothing seems to take away the parse error. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 18, 2013 Share Posted May 18, 2013 Is <p> HTML or PHP? Look at what's different between the code that works. Quote Link to comment Share on other sites More sharing options...
babymac Posted May 18, 2013 Author Share Posted May 18, 2013 I've got it. Thank you so very much for your help. Quote Link to comment Share on other sites More sharing options...
babymac Posted May 18, 2013 Author Share Posted May 18, 2013 I actually have one other question if anyone could explain. The code is doing what I want now, I just don't understand one part of the display. When a field is not entered, the user is shown the statment "Please make sure all input fields are entered." and then the link to go back. When all fields are entered correctly, the user is shown their information and a link to go forward as well as the link to go back. The 'go back' link appears at the top. While I'm happy it's there, it's not included in the if statement for when all fields are not empty. Can someone explain to me why this is? <?php //Create a script that starts a session: session_start(); $_SESSION['fname'] = $_POST['fname']; // First Name $_SESSION['lname'] = $_POST['lname']; // Last Name $_SESSION['email'] = $_POST['email']; // Email Address // Check if the form has been submitted: if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form: if ( (empty($_POST['fname'])) or (empty($_POST['lname'])) or (empty($_POST['email'])) ) // Forgot a field: echo '<p>Please make sure all input fields are entered.</p>'; echo '<a href="http://studentweb.wilmu.edu/sites/lpeer79627/pa2.html"><br />Click to go back and change your information</a>'; } if ( (!empty($_POST['fname'])) && (!empty($_POST['lname'])) && (!empty($_POST['email'])) ) { // Print the received data: echo '<p> You entered your first name as: ' . $_SESSION['fname'] . ' </p> <p> You entered your last name as: ' . $_SESSION['lname'] . ' </p> <p> You entered your email as: ' . $_SESSION['email'] . ' </p>'; echo '<a href="http://studentweb.wilmu.edu/sites/lpeer79627/pa2final.php"><br />Continue to final screen</a>'; } ?> Quote Link to comment Share on other sites More sharing options...
Solution Jessica Posted May 18, 2013 Solution Share Posted May 18, 2013 Use || instead of or 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.