Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/162038-passing-values-between-php-scripts/
Share on other sites

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).

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'];
?>

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.