php_beginner_83 Posted June 13, 2009 Share Posted June 13, 2009 Hi All I'm having problems trying to pass a value between my php scripts. I have scriptA, where the value originates. The value is determined by which link the user clicks. It is a numerical value. This value is passed to the next page, scriptB, through the url. In scriptB, I use $_GET[] to access this value. I also have scriptC. When the user clicks on a link in scriptB, scriptC is displayed in the <div> 'content' section of scriptB, using php include(). I want to pass the value from scriptB to scriptC. From what I understand, when you declare a variable in one php file it is accessible in other php files. I tried this with my code and it didn't work. So I tried explicity setting the value of a variable (instead of getting the value from the url) in scriptB, and it was available in scriptC. I'm really stuck with this. Can anyone suggest a solution, I need to access the value in scriptC but have no idea how to do it. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/162038-passing-values-between-php-scripts/ Share on other sites More sharing options...
.josh Posted June 13, 2009 Share Posted June 13, 2009 you can assign the value to a session variable and it can be accessed from page to page. pagea.php <?php session_start(); $_SESSION['foo'] = 'bar'; ?> pageb.php <?php session_start(); echo $_SESSION['foo']; ?> A session variable will persist for the duration of the browser being open or if it times out due to inactivity (timeout default is 30m). Quote Link to comment https://forums.phpfreaks.com/topic/162038-passing-values-between-php-scripts/#findComment-855001 Share on other sites More sharing options...
jjacquay712 Posted June 13, 2009 Share Posted June 13, 2009 You can pass variables between scripts with $_POST, $_GET, or $_SESSION. I'm not sure what your trying to accomplish so I can't recommend which you should use. You can pass a value from script A to script B easily with $_GET, here is an example: Script A <?php $your_variable = "Hello World!"; echo '<a href="script_b.php?your_variable=' . $your_variable . '">Link</a>'; ?> Script B <?php echo $_GET['your_variable']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/162038-passing-values-between-php-scripts/#findComment-855006 Share on other sites More sharing options...
.josh Posted June 13, 2009 Share Posted June 13, 2009 additionally you can store values in a database, flatfile or cookie, and have retrieve it from there on each page. Overall point is, normal variables only have a scope for the page they are in, unless the page is being included in another page with include or require, because to php that is pretty much the same thing as it being the same script. Quote Link to comment https://forums.phpfreaks.com/topic/162038-passing-values-between-php-scripts/#findComment-855010 Share on other sites More sharing options...
php_beginner_83 Posted June 13, 2009 Author Share Posted June 13, 2009 Thanks for all your replys. I've tried using the $_SESSION[]. Though I have another problem I want to assign a different value to $_SESSION['variable_name'] depending on what link the user clicks. Currently I am using... <td><?php echo "<a href=America1.php?page=main&place=1>"; ?><h3>San Francisco</h3></a> </td> There are a number of these links and the value of 'place' changes. I want to assign this value to $_SESSION. Is there a way I can assign this value depending on what link is clicked. I'm not sure how to go about doing this. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/162038-passing-values-between-php-scripts/#findComment-855035 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.