Verification: a143cc29221c9be0

Pass variable from html to php

Pass variable from html to php

Here is an example of passing data through URL within a site.

link to page2
page2.php idusertom
echo $_GET['id']; // output 2489
echo $_GET['user']; // output tom

Passing data outside

Link to another site
Demo of passing data through a form
?&
URL with Data in a link

Submitting form values through GET method

Passing variables between pages

Submitting a form through POST method

$id=$_POST['id'];
$password=$_POST['password'];
issues GET POST
Browser History Data remain in Browser History Data Not available in Browser History
Bookmark URL with Data can be bookmarked No data is available in URL to bookmark the page
Data Length Restiction The restriction (of URL ) is applicable No Restriction
cached Can be cached No meaningful caching
Sensitive Data Data like password , pin etc are exposed through URL so they should not be passed using GET method Better than GET method as data is not exposed through URL

Read more on difference between GET and POST method of form submission

Collecting data submitted by either GET or POST method

$id=$_REQUEST['id'];
$password=$_REQUEST['password'];

Every scripting language pages has its own way of receiving the data from the URL

$id=$_GET['id'];
$user=$_GET['user'];
Id = Request.QueryString("id")
User = Request.QueryString('user')

Passing data within the site using URL

Passing the data between the site through URL

Linking to a topic

Passing variables through query string

For example let us say we have collected product id from the product table. Read the tutorial on how to collect data from table. Our link with query string will have something like this.

$pid=$nt[product_id];
echo "Product Details";
echo "Product Details";

Chang of data while passing through URL

encode the data before sending them though URLencodeServer.URLEncode(string)

Send variables between Javascript, Form and PHP on the same page:

PHP variable to Javascript variable:

	

Form variable to Javascript variable:

	

PHP variable to Form variable:

	
// PHP code inside HTML!!

Javascript variable to Form variable:

	

Javascript variable to PHP variable:

This is impossible (unless you reload the page, or call another page), since all PHP code is rendered first (on the server side), and then Javascript is dealt with afterwards (on the client side).

Form variable to PHP variable:

Without refreshing, this is impossible like above for similar reasons. If you don't mind a refresh, then either a page reload or calling another web page would work if you used something like $_POST['FormVar']; in the php file.



Allow sending different variables via the URL string to the new page (readable through PHP or Javascript).

The version shown below uses PHP to read the variables back, but it's possible to use Javascript and some messy splitting to do the same (for that route, see this nice little function, or here, or here for example code.)

EXAMPLE:

Send variables via URL!

INSIDE "page1.php" or "page1.html"

	// Send the variables myNumber=1 and myFruit="orange" to the new PHP page...
	Send variables via URL! 

	

INSIDE "page2c.php"

	// Retrieve the URL variables (using PHP).
		$num = $_GET['myNumber'];
		$fruit = $_GET['myFruit'];
		echo "Number: ".$num."  Fruit: ".$fruit;
	?>
	



Maintain a session by sending PHP variables (including multiple full blown multi-dimensional arrays if you so wish) to a page with PHP in.

INSIDE "page1.php"

// "session_register()" and "session_start();" both prepare the session ready for use, and "$myPHPvar=5"
	// is the variable we want to carry over to the new page. Just before we visit the new page, we need to
	// store the variable in PHP's special session area by using "$_SESSION['myPHPvar'] = $myPHPvar;"
	session_register();
	session_start();                      
	$myPHPvar=5;
	$_SESSION['myPHPvar'] = $myPHPvar;
?>

Click this link, and the "$myPHPvar" variable should carry through.

INSIDE "page2.php"

// Retrieve the PHP variable (using PHP).
	session_start();
	$myPHPvar = $_SESSION['myPHPvar'];
	echo "myPHPvar: ".$myPHPvar." ..........(should say \"myPHPvar: 5\")
"; ?>



Allow sending hidden form variables to a PHP page. This allows the user to select options on the page, and for these to be carried across to the new page.

EXAMPLE:

(these links all go to the same PHP page...)
Click 1st
Click 2nd
Click 3rd

Click 1st
Click 2nd
Click 3rd

INSIDE "page1.php" or "page1.html"







Click 1st
Click 2nd
Click 3rd
Click 1st
Click 2nd
Click 3rd

INSIDE "page2.php"

// Retrieve the hidden form variable (using PHP).
	$myvar = $_POST['formVar'];
	echo "myvar: ".$myvar;
?>



Allow sending hidden form variables (using tick boxes) to a PHP page. This allows the user to select options on the page, and for these to be carried across to the new page.

EXAMPLE:

INSIDE "page1.html"

Tick A
Tick B
Tick C

CODE:

INSIDE "page1.html"

Tick A 
Tick B
Tick C

INSIDE "page2b.php"

";
	echo "myvarB: ".$myvarB."
"; echo "myvarC: ".$myvarC; ?>



Allow different bits of HTML to dynamically execute according to which radio button is pressed

This is useful for many purposes including using buttons to change the picture (as part of a gallery), or for different sub forms to appear accordingly.

EXAMPLE:

Click 1st
Click 2nd
Click 3rd

Anything can go here .....[I am the footer]

CODE:

Click 1st
Click 2nd
Click 3rd

Anything can go here .....[I am the footer]



Allow different bits of HTML to dynamically execute according to which dropdown menu is selected

EXAMPLE:

Anything can go here .....[I am the footer]

CODE:





.....[I am the footer] 



Add/remove bits of HTML according to multiple checkboxes

EXAMPLE: Anything can go here

CODE:

  Anything can go here 





Use GET and POST Through HTML Form

HTML form is one of the most powerful features of PHP. Any form element will be automatically available to the action destination of the form.

POST Request

Fetching data to nextPage.php

$username = isset($_POST['username']) ? $_POST['username'] : "";
$email       = isset($_POST['email']) ? $_POST['email'] : "";
echo "Username: ".$username;
echo "Email: ".$email;

The sample output of the script may be:

Username: johndoe
Email: johndoe@gmail.com

The example above shows how to pass a variable using POST through an HTML form. The form element needs to have the action and method attribute. The action contains the next page, in this case, it’s nextPage.php. The method can be a POST or GET. You can then access the elements in the nextPage.php using a $_POSTor $_GET.

GET Request

Bring me to nextPage

This example will create a GET variable and can be accessed on nextPage.php.

Example:

echo $phpVariable = $_GET['phpVariable'];
//output: Dog

The GET can be accessed using $_GET

Another way is to add a hidden element in an HTML form that submits to the next page.

Example:

nextPage.php

//Using POST
$phpVariable = $_POST['phpVariable'];
//Using GET
$phpVariable = $_GET['phpVariable'];
//Using GET, POST or COOKIE;
$phpVariable = $_REQUEST['phpVariable'];

You can change the method from POST to GET to use the GET request. POST and GET are bot insecure, but GET is easier to hack since it’s available through the front-end.

The $_REQUEST can both accept GET, POST or COOKIE. It’s good to use $_REQUEST on self-referential forms for validations.

Use session and cookie

The session and cookie are easier to use, but the session is far more secure than the cookies, but not completely secure.

session

//page 1
$phpVariable = "Dog";
$_SESSION['animal'] = $phpVariable;

//page 2
$value = $_SESSION['animal'];

Note

When using the session, always remember to add session_start() on both pages before accessing the $_SESSION array.

//page 1
$phpVariable = "Dog";
$_COOKIE['animal'] = $phpVariable;

//page 2
$value = $_COOKIE['animal'];

The most obvious difference between cookies and session is that that session will be stored on the server-side while the cookies will have the client side as the storage.

Contribute

DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Use GET and POST Through HTML Form

HTML form is one of the most powerful features of PHP. Any form element will be automatically available to the action destination of the form.

POST Request

Fetching data to nextPage.php

$username = isset($_POST['username']) ? $_POST['username'] : "";
$email       = isset($_POST['email']) ? $_POST['email'] : "";
echo "Username: ".$username;
echo "Email: ".$email;

The sample output of the script may be:

Username: johndoe
Email: johndoe@gmail.com

The example above shows how to pass a variable using POST through an HTML form. The form element needs to have the action and method attribute. The action contains the next page, in this case, it’s nextPage.php. The method can be a POST or GET. You can then access the elements in the nextPage.php using a $_POSTor $_GET.

GET Request

Bring me to nextPage

This example will create a GET variable and can be accessed on nextPage.php.

Example:

echo $phpVariable = $_GET['phpVariable'];
//output: Dog

The GET can be accessed using $_GET

Another way is to add a hidden element in an HTML form that submits to the next page.

Example:

nextPage.php

//Using POST
$phpVariable = $_POST['phpVariable'];
//Using GET
$phpVariable = $_GET['phpVariable'];
//Using GET, POST or COOKIE;
$phpVariable = $_REQUEST['phpVariable'];

You can change the method from POST to GET to use the GET request. POST and GET are bot insecure, but GET is easier to hack since it’s available through the front-end.

The $_REQUEST can both accept GET, POST or COOKIE. It’s good to use $_REQUEST on self-referential forms for validations.

Use session and cookie

The session and cookie are easier to use, but the session is far more secure than the cookies, but not completely secure.

session

//page 1
$phpVariable = "Dog";
$_SESSION['animal'] = $phpVariable;

//page 2
$value = $_SESSION['animal'];

Note

When using the session, always remember to add session_start() on both pages before accessing the $_SESSION array.

//page 1
$phpVariable = "Dog";
$_COOKIE['animal'] = $phpVariable;

//page 2
$value = $_COOKIE['animal'];

The most obvious difference between cookies and session is that that session will be stored on the server-side while the cookies will have the client side as the storage.

Contribute

DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.