Asked  7 Months ago    Answers:  5   Viewed   376 times

It seems pretty simple but I can't find a good way to do it.

Say in the first page I create a variable

$myVariable = "Some text";

And the form's action for that page is "Page2.php". So in Page2.php, how can I have access to that variable? I know I can do it with sessions but I think it's too much for a simple string, and I do only need to pass a simple string (a file name).

How can I achieve this?




HTML / HTTP is stateless, in other words, what you did / saw on the previous page, is completely unconnected with the current page. Except if you use something like sessions, cookies or GET / POST variables. Sessions and cookies are quite easy to use, with session being by far more secure than cookies. More secure, but not completely secure.


//On page 1
$_SESSION['varname'] = $var_value;

//On page 2
$var_value = $_SESSION['varname'];

Remember to run the session_start(); statement on both these pages before you try to access the $_SESSION array, and also before any output is sent to the browser.


//One page 1
$_COOKIE['varname'] = $var_value;

//On page 2
$var_value = $_COOKIE['varname'];

The big difference between sessions and cookies is that the value of the variable will be stored on the server if you're using sessions, and on the client if you're using cookies. I can't think of any good reason to use cookies instead of sessions, except if you want data to persist between sessions, but even then it's perhaps better to store it in a DB, and retrieve it based on a username or id.


You can add the variable in the link to the next page:

<a href="page2.php?varname=<?php echo $var_value ?>">Page2</a>

This will create a GET variable.

Another way is to include a hidden field in a form that submits to page two:

<form method="get" action="page2.php">
    <input type="hidden" name="varname" value="var_value">
    <input type="submit">

And then on page two:

//Using GET
$var_value = $_GET['varname'];

//Using POST
$var_value = $_POST['varname'];

//Using GET, POST or COOKIE.
$var_value = $_REQUEST['varname'];

Just change the method for the form to post if you want to do it via post. Both are equally insecure, although GET is easier to hack.

The fact that each new request is, except for session data, a totally new instance of the script caught me when I first started coding in PHP. Once you get used to it, it's quite simple though.

Wednesday, March 31, 2021
answered 7 Months ago

You need to call session_start(); on both pages.

Wednesday, March 31, 2021
answered 7 Months ago

Your ternary statement is currently only checking if $_SESSION['vip'] is set. The problem is that it is set no matter what the user chooses ("yes" or "no") because in the part where you set the session variable, the conditional statement sets the session variable if $_POST['vip'] isn't empty.

Try checking if it's equal to "Yes" like this:

<b>Total Reservation Costs: </b><br><br> £
     if (isset($_SESSION['party']) && is_numeric($_SESSION['party'])) {
         $party = (int)$_SESSION['party'];
         $vip = (isset($_SESSION['vip']) && $_SESSION['vip'] === 'Yes') ? 5 : 0;
         echo (($party * 5) + $vip);
Wednesday, March 31, 2021
answered 7 Months ago

Use the header function

$id = $user_profile['id'];
$_SESSION['id'] = $id;
header('Location: checkIfExsists.php?id='.$id);

on the checkIfExsists.php get the variable with

$id = $_GET["id"];

That would solve your problem the way you want it to be solved, but, this isn´t neccesarilly the way it should be solved, maybe inside checkIfExists.php should be a class instead of structured code with a public function to check existance checkExistance, so you will then just need:

$check = new checker();
$exists = $check->checkExistance($id) ;

this way you do not have to be jumping between files and you can have a better way to re-use code, regards.

Wednesday, March 31, 2021
answered 7 Months ago

Use string interpolation with double quotes for the echo statement and single quotes everywhere inside the javascript:

echo "<script type='text/javascript' language='javascript'>'$file'); 

The interpolated PHP variable $file should be correctly interpreted as a string and the value it holds should be displayed in the URI of your javascript. Check out this easy to understand info about variable interpolation

Saturday, May 29, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :