Saturday, 16 March 2019

Form Variable of PHP

 php form Variables are used to get the data submitted through a html form. and in a html form there is only 2 method and they are get and post method.

so through html form we can send the data in 2 different method and they are get method and post method. and the example is given below,

Html Form With Post Method:

<html>
<body>
<form action="welcome_post.php" method="post">
Name: <input type="text" name="name">
Contact No: <input type="text" name="contact_no">
<input type="submit">
</form>
</body>
</html>

And Html Form With Get Method,

<html>
<body>
<form action="welcome_get.php" method="get">
Name: <input type="text" name="name">
Contact No: <input type="text" name="contact_no">
<input type="submit">
</form>
</body>
</html>

How to get php Form Variables

There is 3 different procedure to get the php form Variable and they are $_POST to get the post data send by a html form with post method, $_GET to get the data send by a html form by get method and in both method data can be get by $_REQUEST .
The $_POST, $_GET and $_REQUEST all are array type and super global Variable , that is it can be get inside or outside of scope ar in another word it can be say thet the ceriable can be fetched inside and outside of function in the page where the date is sent.
in all 3 cases the $_GET or $_POST or $_REQUEST return a array and according to the upper html example it will be array( 'name' => 'name input value', 'contact_no' => 'contact no input value')

Now theexample of how to get the php  Get Method php form Variable Example:

<?php
echo 'Name= ' . $_GET["name"] . '<br/>';
echo 'And Contact No = ' . $_GET["contact_no"];
?>

Post Method php form Variable Example:

<?php
echo 'Name= ' . $_POST["name"] . '<br/>';
echo 'And Contact No = ' . $_POST["contact_no"];
?>

Get and Post both canbe fetched by php form Variable Example:

<?php
echo 'Name= ' . $_REQUEST["name"] . '<br/>';
echo 'And Contact No = ' . $_REQUEST["contact_no"];
?>

Differents between GET and POST method,

Both GET and POST create an array. The array holds key/value, where keys are the names of the form input fields and values are the data given as input in the form fields by the user.
Both GET and POST are treated as $_GET and $_POST. These are superglobals, That is they are accesable inside or outside of scope or functions.
$_GET is an array of variables passed through the URL which is visible to all user.
$_POST is an array of variables passed through the HTTP POST method which is not visible by all user.
And also it can be said that through get method we can send less amount of data where through post method we can send greter ammount of data.
The amount of data sent through get method is dependable on browser and php both as php have its data limit and browser have its url lenght limit. but the data sent through post method is depend on the php only thet how much data limit is there.
Thanks for reading the PHP Form Variables post. Hope you are doing well.

No comments:

Post a Comment