Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I get a variable from one file to another in php

Tags:

html

php

I have 3 files. I will give you the exact example:

a.php

<form action="b.php" method="POST">

Enter age:
<input type="text" name="age"><br>
<input type="submit" value="Save">
</form>

b.php

<?php
$age=$_POST["age"];
if (is_numeric($age))
{
    header("Location: c.php");
    exit();
}
else
{
    echo "Age invalid!";
}
?>

c.php

<?php
//i want to use the $age variable here   
echo $age;
?>

How can I use the $age variable from b.php in c.php?

I also tried to session_start(); at file b.php and use $_SESSION["age"]=$_POST["age"]; in b.php and then $_SESSION["age"] in c.php in stead of $age and it still didn't work.

I also tried include but didn't get me anywhere either... maybe I didn't use it correctly.

like image 413
Point89 Avatar asked Dec 09 '25 15:12

Point89


1 Answers

The issue is that in file b.php, you are not sending the value of age to c.php.

If, in b.php, you did this:

 session_start();
 $_SESSION["age"] = $_POST["age"];

Then, in c.php, you did this:

 session_start();
 $age = $_SESSION["age"];
 echo $age;

It will work properly.

like image 196
random_user_name Avatar answered Dec 12 '25 05:12

random_user_name



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!