PHP Pass Variable to Next Page

PHP Pass variable to next page

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.

Session:

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

Cookie:

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

GET and POST

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">
</form>

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.

Pass the variable to next page PHP

first you need to do put session_start() top of the script.and always remember $_SESSION is super global variable.no need to pass here to access session variable.simply you can access like this.

echo $_SESSION['name'];

Note:make sure session_start() should be top of the script.

if you really want to pass variable one page to another page.do something like below

<td> <a href="more_info.php?name=<?php echo $_SESSION['name']; ?>" >More Info</a></td> 

And your more_info.php.

if (isset($_GET['name'])) {
$name=$_GET['name'];
echo $name;
}

given answer could be solve the OP problem but.this is not good habit to use session in while loop.because you are trying to insert name in session but name could be different.i recommend to you try something like this.

Remove:

if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$_SESSION['name'] = $name;
$_SESSION['phone'] = $phone;
$_SESSION['city'] = $city;

And pass the variable like this.

<td> <a href="more_info.php?name=<?php echo $name; ?>" >More Info</a></td> 

Pass variable in php to next page from a table

You have code trying to set a session inside your echo. You want it separate:

echo "<td><form action=player_profile.php>
<input name=id type=hidden value='".$row['PlayerID']."'>
<input type=submit name=submit value=info>
</form></td>";
echo "</tr>";

If you want to access the PlayerID variable on the player_profile.php you just use:

$var_value = $_GET['id'];

You could also put it into a session var at that point, but I don't know why you would need that.

Passing variables to another page with url - PHP

You set $_SESSION['status'] to 0 and then set it to 1, so it will always be 1. Additionally, the link you click has nothing to do with the session vars.

Page 1:

Normally to get just one variable from one page to another via hyperlink you would add it to the URL as a query parameter and access it using $_GET:

<?php
echo '<a href="page2.php?status=0">No</a>';

echo '<a href="page2.php?status=1">Yes</a>';
?>

Page 2:

You also need to check if $_GET['status'] is set:

<?php
if (isset($_GET['status']) && $_GET['status'] == 0) {
echo "No !";
}
elseif (isset($_GET['status']) && $_GET['status'] == 1) {
echo "Yes !";
}
else {
echo "NOT set !";
}
?>

Now, if you want it to be accessible on other pages without passing in the URL, then set it as a session var:

    session_start();
$_SESSION['status'] = $_GET['status'];

Pass variables between two PHP pages without using a form or the URL of page

Sessions would be good choice for you. Take a look at these two examples from PHP Manual:

Code of page1.php

<?php
// page1.php

session_start();

echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>

Code of page2.php

<?php
// page2.php

session_start();

echo 'Welcome to page #2<br />';

echo $_SESSION['favcolor']; // green
echo $_SESSION['animal']; // cat
echo date('Y m d H:i:s', $_SESSION['time']);

// You may want to use SID here, like we did in page1.php
echo '<br /><a href="page1.php">page 1</a>';
?>

To clear up things - SID is PHP's predefined constant which contains session name and its id. Example SID value:

PHPSESSID=d78d0851898450eb6aa1e6b1d2a484f1

Passing variables to next page through $_SESSION

After using header() function, you shall use exit; function. More details: PHP session destroyed / lost after header

Pass post variables to next page

JiteshNK provided one way of doing it using sessions.

Me, personally, I don't like to waste session variables on trivial things.

So another way you can do this is when you submit to your preview page (let's call preview.php), store the values in hidden input fields.

preview.php

<?php
if (isset($_POST["preview"])) {
// get data
$var1 = mysqli_real_escape_string($link, $_POST['var1']);
$var2 = mysqli_real_escape_string($link, $_POST['var2']);
// etc
?>

<!-- show vars -->

<form action="insert-page.php">
<!-- store vars into hidden input fields -->
<input type="hidden" name="var1" value="<?= $var1 ?>">
<input type="hidden" name="var2" value="<?= $var2 ?>">
<!-- etc -->
<p><a onclick="goBack()" class="btn btn-block btn-warning">Dont like what you see? Go back and fix it.</a></p>
<p><input type="submit" name="good" class="btn btn-block btn-success">All good!</a></p><!-- use submit button instead -->
</form>


Related Topics



Leave a reply



Submit