mrneilrobinson Posted July 26, 2009 Share Posted July 26, 2009 hi there, i am trying to create a story for kids, that collects different information on each page, ie, the first page collects name, then goes to age.php, which collects age, then to gender etc. Whats the best way of using one of the variables on a later page? ie the first age collects a boys name called ben, i wish to say in the third page something like: ben, are you a boy or a girl. cheers Quote Link to comment https://forums.phpfreaks.com/topic/167510-variables-on-different-pages/ Share on other sites More sharing options...
RichardRotterdam Posted July 26, 2009 Share Posted July 26, 2009 There are a number of ways you can do that. You can use sessions, cookies, send parameters along with the url and a couple more ways. Just look into those subjects and see which one will suit your needs the best. Quote Link to comment https://forums.phpfreaks.com/topic/167510-variables-on-different-pages/#findComment-883322 Share on other sites More sharing options...
vineld Posted July 26, 2009 Share Posted July 26, 2009 Saving the data in a session variable sounds good. You might want to save it to a database though so that you can print out and look at previous stories. That will give you some stats options as well in the future. Quote Link to comment https://forums.phpfreaks.com/topic/167510-variables-on-different-pages/#findComment-883323 Share on other sites More sharing options...
mrneilrobinson Posted July 26, 2009 Author Share Posted July 26, 2009 is it possible to use sessions when the data is posted because im struggling with that! do i have the code correct below? <?php session_start ();?> <head>... <body> <?php $name = $_POST['name']; ?> <?php $_POST['name'] = $_SESSION['name']?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/167510-variables-on-different-pages/#findComment-883370 Share on other sites More sharing options...
vineld Posted July 26, 2009 Share Posted July 26, 2009 The $_POST array automatically holds the values that you submitted from a form. What you do now is that you retrieve the form value and assign it to the variable $name. What you do next is that you assign the $_POST["name"] variable with the value stored in $_SESSION["name"]. You would want to do the opposite I assume: $_SESSION["name"] = $name; You don't really need $name though: $_SESSION["name"] = $_POST["name"]; You might want to do some validation though on the posted values, depending on how you use them. Quote Link to comment https://forums.phpfreaks.com/topic/167510-variables-on-different-pages/#findComment-883376 Share on other sites More sharing options...
mrneilrobinson Posted July 26, 2009 Author Share Posted July 26, 2009 brilliant thankyou do i need to use both lines of code? and do i need to assign it on just the page that i need the variable on? or once at the start which will cover all pages? cheers Quote Link to comment https://forums.phpfreaks.com/topic/167510-variables-on-different-pages/#findComment-883382 Share on other sites More sharing options...
vineld Posted July 26, 2009 Share Posted July 26, 2009 No, the two lines of code do the exact same thing. I just wanted to demonstrate that you didn't need that extra variable for this. As long as the session doesn't time out you only need to set the variable once. If the story is long you may want to add some features in order to make the application more user friendly though. Now, if the browser crashes (or they happen to close it for some other reason), they are not able to continue from where they were at. Children are usually more unpredictable than adults. Quote Link to comment https://forums.phpfreaks.com/topic/167510-variables-on-different-pages/#findComment-883402 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.