PHP Variables Form


 

PHP Variables Form

If you want to use one variable into multiple pages you will have to opt one out of three methods :

If you want to use one variable into multiple pages you will have to opt one out of three methods :

PHP Variables Form

If you want to use one variable into multiple pages you will have to opt one out of three methods : $_GET[], $_POST[] and $_REQUEST[] method. Let's first see the example : 

Example 1 : $_GET[] ;

Page1 :( login.php)

<html>

<head>

<title>PHP Variables Form</title>

</head>

<body>

<form action="welcome.php" method="get">

    Username : <input type="text" name="username" value=""></input>

    Password : <input type="password" name="pass" value=""></input>

    <input type="submit" name="sumbit" value="Submit me!"></input>

</form>

</body>

</html>

Page 2 : welcome.php

<?php

$name = $_GET['username'];

$pass = $_GET['pass'];

    echo "Your username is $name ."."<br/>";

    echo "Your password is $pass .";

?>

In this example, we have used two files - first one is your basic html code file and the second file is where we want to send the information through $_GET method. Through mentioning method in the form we can transfers the information from one page to another. Here, we have used $_GET method to send the information to the other page.

Note that $_GET method send the whole information in the above URL and it is not advisable to use this method because anyone can retrieve your information through URL. For example: If you are signing up for any site and entering your username and password and submit the form, the whole information looks in the URL and anybody can take it from there. It can be shown like this : 

http://localhost/rakesh/welcome.php?username=rakesh.pandey10%40gmail.com&pass=12365&sumbit=Submit+me!



Example 2 :  $_POST[ ];    

Page 1 : login.php

<html>

<head>

<title>PHP Variables Form</title>

</head>

<body>

<form action="welcome.php" method="post">

    Username : <input type="text" size="25" name="username" value="" />

0

    Password : <input type="password" size="25" name="pass" value="" />

    <input type="submit" name="sumbit" value="Submit me!" />

</form>

1

</body>

</html>

 

2

Page 2 : welcome.php

<?php

$name = $_POST['username'];

3

$pass = $_POST['pass'];

echo "Your username is $name ."."<br/>";

echo "Your password is $pass .";

4

?>

Here in this example we have used $_POST method to transfer the variable from one page to another. Information sent from the form cannot be retrieved because it doesn't visible anywhere. It is the most secure method to use. It looks like it : http://localhost/rakesh/welcome.php

 

5

Example 3 : $_REQUEST()

Page 1 : login.php

<html>

6

<head>

<title>php variables across pages</title>

</head>

7

<body>

<form action = "welcome.php" method = "post">

    Username : <input type="text" size="25" name="username" value="" />

8

    Password : <input type="password" size="25" name="pass" value="" />

    <input type="submit" name="sumbit" value="Submit me!" />

</form>

9

</body>

</html> 

Page 2 : welcome.php

0

<?php

$name = $_REQUEST['username'];

$pass = $_REQUEST['pass'];

1

echo "Your username is $name ."."<br/>";

echo "Your password is $pass .";

?>

2

The third method of transfering the variable from one page to another using single variable is $_REQUEST, which contains the both $_GET method and $_POST method. $_REQUEST() function is used for fetching the information from the both $_GET and $_POST method. It is used when we are not sure about the appropriate method.


Ads