Skip to content Skip to sidebar Skip to footer

How To Pass Variable Through Without Form

I am trying to maintain a custom built PHP roster system. I'm trying to pass a variable along to a second PHP page. Previously, this was being done through a form with a dropdown:

Solution 1:

pass variables through Url, href="localhost/myphp.php?name=jake" and use GET in the myphp.php file to get the var, echo $_GET["name"];//jake

Solution 2:

Here are the following solutions to your problem :

GET :

Use GET to pass variable to your another page :

Here as :

<ahref="/awardedit.php?username=<?phpecho$username; ?>" >Submit Username</a>

Note : Now it becomes a link when you click on it so it passes your username variable data to your awardedit.php page.

And then on that page you can catch the variable value using the following code:

<?php$username = $_GET["username"];
//And then do whatever you want to do with it?>

If you want to do it this way so then there you can use the following way to do this :

COOKIES:

Use COOKIES if you don't want to do anything unsecure :

setcookie("username", $username, time()+5);
header("Location: awardedit.php");

And then fetch the cookie at your awardedit.php page as :

if (isset($_COOKIE['username'])) {
$username = $_COOKIE['username'];
}

Still if you don't want to do it this way so you can do it the following way and it's more secure way :

SESSIONS :

Set a session like this way :

$_SESSION['username'] = $username;

And on your awardedit.php page to fetch the value.you can do it like this way :

$username = $_SESSION['username'];
echo$username;

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.

Post a Comment for "How To Pass Variable Through Without Form"