garyed Posted May 30, 2013 Share Posted May 30, 2013 Is it possible to post the input of a form to the existing php page & at the same time post it to a new php page you want the user to go to when they submit the form? Quote Link to comment https://forums.phpfreaks.com/topic/278601-post-to-more-than-one-page/ Share on other sites More sharing options...
cyberRobot Posted May 30, 2013 Share Posted May 30, 2013 You sure can Quote Link to comment https://forums.phpfreaks.com/topic/278601-post-to-more-than-one-page/#findComment-1433183 Share on other sites More sharing options...
Hughesy1986 Posted May 30, 2013 Share Posted May 30, 2013 If I was to create something like this I would use CURL to post the data that was posted to the first form. http://php.net/manual/en/book.curl.php Quote Link to comment https://forums.phpfreaks.com/topic/278601-post-to-more-than-one-page/#findComment-1433185 Share on other sites More sharing options...
garyed Posted May 30, 2013 Author Share Posted May 30, 2013 If I was to create something like this I would use CURL to post the data that was posted to the first form. http://php.net/manual/en/book.curl.php Doing some reading up on CURL, if I understand it right. The first page would just do a standard SUBMIT on the form & POST to the second page. Then I would use CURL on the second page so when it loaded, it would open the first page & post the data back into the first page. Does that sound right? Quote Link to comment https://forums.phpfreaks.com/topic/278601-post-to-more-than-one-page/#findComment-1433192 Share on other sites More sharing options...
garyed Posted May 30, 2013 Author Share Posted May 30, 2013 Is there any other ways besides using CURL? If so I'd appreciate any other ideas so I can try to see what way works best for my situation. Quote Link to comment https://forums.phpfreaks.com/topic/278601-post-to-more-than-one-page/#findComment-1433193 Share on other sites More sharing options...
cyberRobot Posted May 30, 2013 Share Posted May 30, 2013 (edited) You could store the form information within a database and grab it as needed. Could you provide a little more information on what you're trying to do? Edited May 30, 2013 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/278601-post-to-more-than-one-page/#findComment-1433194 Share on other sites More sharing options...
davidannis Posted May 30, 2013 Share Posted May 30, 2013 Are you trying to have the two pages open in separate tabs in the user's browser? Quote Link to comment https://forums.phpfreaks.com/topic/278601-post-to-more-than-one-page/#findComment-1433202 Share on other sites More sharing options...
garyed Posted May 30, 2013 Author Share Posted May 30, 2013 What I'm trying to do is this: I've written a calculation program that the user chooses some dropdown menu items & a few other inputs in a form from the page. When the form is submitted it takes them to another page where all the data from the first page is posted into variables on the second page & used for further calculations with other inputs. I'm using session variables on both pages. When they leave the first page the only way I know to get the session variables to the first page is to use a form on the second page that posts back to the first page. If they leave the second page without submitting the form then all the data on the first page will be lost. The reason I'm using separate pages is to keep the pages from getting too cluttered since there is so much data that the user inputs. So what I'm trying to do is figure a way that when the user submits the form on the first page the variables get posted onto that page & then takes the user to the second page where the same variables will be posted too. Quote Link to comment https://forums.phpfreaks.com/topic/278601-post-to-more-than-one-page/#findComment-1433290 Share on other sites More sharing options...
davidannis Posted May 31, 2013 Share Posted May 31, 2013 I would save the data to a database and when they go to the first page check to see if a session exists and if it does retrieve the data and fill the form. Quote Link to comment https://forums.phpfreaks.com/topic/278601-post-to-more-than-one-page/#findComment-1433337 Share on other sites More sharing options...
garyed Posted June 7, 2013 Author Share Posted June 7, 2013 Thanks everyone for the input, It seems like everything i tried didn't work & I'm sure it was because I couldn't understand how to use Curl correctly or the database thing so I figured out a simple way that works for my situation. Since I'm using session variables all I had to do was use hidden inputs on the second page for the variables from the first page. I got some help here earlier with a session function that I use on both pages. It's not very pretty but here it is: Page1.php <?php session_start(); function get_value($var) { if ($_POST[$var]!="" || (!empty($_SESSION[$var]) && $_POST[$var] === '')) { $_SESSION[$var]=$_POST[$var]; } if (isset($_SESSION[$var])){ return $_SESSION[$var];}else{ return $_POST[$var];} } ?> <br> <form method="POST" action="page2.php"> Wall type : <select name="wall" > <option>----</option> <option value="green" <?php if(get_value('wall') == "green") { echo 'selected="selected"'; } ?> >green </option> <option value="blue" <?php if(get_value('wall') == "blue") { echo 'selected="selected"'; } ?> >blue </option> </select> <input type="submit" value="Submit"> </form> Page2.php <?php session_start(); function get_value($var) { if ($_POST[$var]!="" || (!empty($_SESSION[$var]) && $_POST[$var] === '')) { $_SESSION[$var]=$_POST[$var]; } if (isset($_SESSION[$var])){ return $_SESSION[$var];}else{ return $_POST[$var];} } ?> <br> <form method="POST" action=""> Door : <select name="door" > <option>----</option> <option value="white" <?php if(get_value('door') == "white") { echo 'selected="selected"'; } ?> >white </option> <option value="red" <?php if(get_value('door') == "red") { echo 'selected="selected"'; } ?> >red </option> </select> <input type="hidden" value="<?php get_value('wall'); ?>" > <input type="submit" value="Submit"> </form> Thanks again for all the help Quote Link to comment https://forums.phpfreaks.com/topic/278601-post-to-more-than-one-page/#findComment-1434639 Share on other sites More sharing options...
garyed Posted June 7, 2013 Author Share Posted June 7, 2013 I also see now that I didn't need to use hidden inputs. I could have just called the functions for each variable i wanted to keep when I opened page2.php. Quote Link to comment https://forums.phpfreaks.com/topic/278601-post-to-more-than-one-page/#findComment-1434644 Share on other sites More sharing options...
cyberRobot Posted June 7, 2013 Share Posted June 7, 2013 I also see now that I didn't need to use hidden inputs. I could have just called the functions for each variable i wanted to keep when I opened page2.php. Does that mean the forms work as you expect now? Side note: I would recommend using the <label> tag for your form labels. For more information, visit http://www.cyberscorpion.com/2012-02/making-html-forms-more-accessible-and-improving-usability-with-the-label-tag/ Quote Link to comment https://forums.phpfreaks.com/topic/278601-post-to-more-than-one-page/#findComment-1434659 Share on other sites More sharing options...
davidannis Posted June 7, 2013 Share Posted June 7, 2013 Instead of calling the function each time: Door : <select name="door" > <option>----</option> <option value="white" <?php if(get_value('door') == "white") { echo 'selected="selected"'; } ?> >white </option> <option value="red" <?php if(get_value('door') == "red") { echo 'selected="selected"'; } ?> >red </option> </select> you can call it once and use the return value multiple times which will be more efficient: Door : <select name="door" > <?php $door_val=get_value('door')?> <option>----</option> <option value="white" <?php if($door_value == "white") { echo 'selected="selected"'; } ?> >white </option> <option value="red" <?php if($door_value == "red") { echo 'selected="selected"'; } ?> >red </option> </select> another strategy is to store the values of the colors in an array and then loop through it to create the select. That way, if you want to add 20 more colors you just add them once and don;t have a ton of code: Door : <select name="door" > <?php $door_val=get_value('door'); $door_colors=Array ('blue','green','red'); ?> <option>----</option> <?php foreach ($door_colors as $color){ echo '<option value="'.$color.'" '; if ($color == $door_val) echo "selected=\"selected\""; echo ">$color </option>"; } Quote Link to comment https://forums.phpfreaks.com/topic/278601-post-to-more-than-one-page/#findComment-1434683 Share on other sites More sharing options...
garyed Posted June 7, 2013 Author Share Posted June 7, 2013 Does that mean the forms work as you expect now? Side note: I would recommend using the <label> tag for your form labels. For more information, visit http://www.cyberscorpion.com/2012-02/making-html-forms-more-accessible-and-improving-usability-with-the-label-tag/ Yes it does. I'm assume it's not the correct way to do it but it works perfectly now. Quote Link to comment https://forums.phpfreaks.com/topic/278601-post-to-more-than-one-page/#findComment-1434732 Share on other sites More sharing options...
garyed Posted June 7, 2013 Author Share Posted June 7, 2013 Instead of calling the function each time: Door : <select name="door" > <option>----</option> <option value="white" <?php if(get_value('door') == "white") { echo 'selected="selected"'; } ?> >white </option> <option value="red" <?php if(get_value('door') == "red") { echo 'selected="selected"'; } ?> >red </option> </select> you can call it once and use the return value multiple times which will be more efficient: Door : <select name="door" > <?php $door_val=get_value('door')?> <option>----</option> <option value="white" <?php if($door_value == "white") { echo 'selected="selected"'; } ?> >white </option> <option value="red" <?php if($door_value == "red") { echo 'selected="selected"'; } ?> >red </option> </select> another strategy is to store the values of the colors in an array and then loop through it to create the select. That way, if you want to add 20 more colors you just add them once and don;t have a ton of code: Door : <select name="door" > <?php $door_val=get_value('door'); $door_colors=Array ('blue','green','red'); ?> <option>----</option> <?php foreach ($door_colors as $color){ echo '<option value="'.$color.'" '; if ($color == $door_val) echo "selected=\"selected\""; echo ">$color </option>"; } That's an interesting way to do it. I used the code that I posted to test before I changed the code on the actual site. It's got multiple dropdowns with tons of choices that are all populated from a mysql database. Thanks again to everyone for all the help. Quote Link to comment https://forums.phpfreaks.com/topic/278601-post-to-more-than-one-page/#findComment-1434733 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.