March 11, 2012

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:

  1. $_GET

    Pass the data in the form of url page.php?variable=value and
    read the values as `echo $_GET[‘variable’]

  2. $_POST

    Pass the data using <form method="POST">...</form>
    and read the values as echo $_POST['variable']

  3. $_SESSION

    1. Start the session session_start();
    2. Declare a variable $_SESSION['variable'] = "value";
    3. Read the value echo $_SESSION['variable'];
  4. $_SERVER

    1. Declare a variable $_SERVER['variable'] = "value";
    2. Read the value echo $_SERVER['variable'];

Each of the above methods are fit in their own cases. To read more go here.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!