PHP Transfer of variables from one page to another
Question by Daniel USAF
I got my first PHP form working, and it submits to the database before returning to the form. But the form does not reflect the submitted values. I remember how to inject dynamic HTML based on PHP variables, but my understanding is that first there is a way to transfer php variable values from one page to another. How do you do this? Here is the end of my submission page, just in case it helps.
$STH = $DBH->prepare("UPDATE administration SET ac1= ?, ac2= ?, fan= ?, na= ?, dh= ?, tolerance1= ?, temptime1= ?, tolerance2= ?, temptime2= ?, tolerance3= ?, temptime3= ?, tolerance4= ?, temptime4= ?, tolerance5= ?, temptime5= ?, humidtolerance1= ?, humidtime1= ?, humidtolerance2= ?, humidtime2= ?, humidtolerance3= ?, humidtime3= ?, humidtolerance4= ?, humidtime4= ?, humidtolerance5= ?, humidtime5= ? WHERE custnum = ?");
$STH->execute(array($ac1, $ac2, $fan, $na, $dh, $tolerance1, $temptime1, $tolerance2, $temptime2, $tolerance3, $temptime3, $tolerance4, $temptime4, $tolerance5, $temptime5, $humidtolerance1, $humidtime1, $humidtolerance2, $humidtime2, $humidtolerance3, $humidtime3, $humidtolerance4, $humidtime4, $humidtolerance5, $humidtime5, $custnum));
$STH->execute();
//Send them back to the page they were at/
header("location:index.php");
Answer by Starx
Use super global variables to pass the data from one page to another. There are many but some of the most popular are:
-
Pass the data in the form of url
page.php?variable=value
and
read the values as `echo $_GET[‘variable’] -
Pass the data using
<form method="POST">...</form>
and read the values asecho $_POST['variable']
-
- Start the session
session_start();
- Declare a variable
$_SESSION['variable'] = "value";
- Read the value
echo $_SESSION['variable'];
- Start the session
-
- Declare a variable
$_SERVER['variable'] = "value";
- Read the value
echo $_SERVER['variable'];
- Declare a variable
Each of the above methods are fit in their own cases. To read more go here.