Javascript pass the value of a variable. Passing Variables in PHP. GET and POST methods

8 answers

HTML/HTTP is stateless, in other words, what you did/saw on previous page, completely unrelated current page. Except when you're using something like sessions, cookies, or GET/POST. Sessions and cookies are quite easy to use, and the session is much 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"];

Don't forget to run session_start(); on both of these pages, before attempting to access the $_SESSION array, and also before any output is sent to the browser.

//One page 1 $_COOKIE["varname"] = $var_value; //On page 2 $var_value = $_COOKIE["varname"];

A big difference between sessions and cookies is that the value of the variable will be stored on the server if you are using sessions and on the client if you are using cookies. I can't think of any good reason to use cookies instead of sessions, unless you want the data to persist between sessions, but even then it might be better to store it in the DB and retrieve it based on the username or ID.

GET and POST