All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Form Handling

It is very easy to send and collect user data with forms and with PHP we can collect and process user data as per our need.There are two ways the user can send information to the webserver.


  • With The GET Method
  • With The POST Method


Form Example With PHP GET Method


<!--This is the demo.html file-->

<html>
<body>

<form method="GET" action="recieve.php">

<input type="text" name="text1">
<input type="submit" name="submit">

</form>

</body>
</html>


/*recieve.php file content*/

echo "Your Name is ".$_GET['text1'];


Code Explanation

  • In demo.html file we want to enter the name of the user when he enter the name and then submit the form all the form data goes to recieve.php file.
  • In recieve.php file we get the form data by $_GET['variableName']; and then display the data with the help of echo method.



Form Example With PHP POST Method


<!--This is the demo.html file-->

<html>
<body>

<form method="POST" action="recieve.php">

<input type="text" name="text1">
<input type="submit" name="submit">

</form>

</body>
</html>


/*recieve.php file content*/

echo "Your Name is ".$_POST['text1'];


Code Explanation

  • In demo.html file we want to enter the name of the user when he enter the name and then submit the form all the form data goes to recieve.php file.
  • In recieve.php file we get the form data by $_POST['variableName']; and then display the data with the help of echo method.



Form Example With PHP REQUEST Method

The PHP $_REQUEST variable contains the contents of both $_GET, $_POST.



<!--This is the demo.html file-->

<html>
<body>

<form method="POST" action="recieve.php">

<input type="text" name="text1">
<input type="submit" name="submit">

</form>

</body>
</html>


/*recieve.php file content*/

echo "Your Name is ".$_REQUEST['text1'];


Code Explanation

  • In demo.html file we want to enter the name of the user when he enter the name and then submit the form all the form data goes to recieve.php file.
  • In recieve.php file we get the form data by $_REQUEST['variableName']; and then display the data with the help of echo method.


Difference Between GET and POST Methods

  • User data send with the GET method is visible to everyone all variable names and values are displayed in the URL.
  • Information sent from a form with the POST method is invisible to others via the HTTP request.
  • You can send multi-part binary input while uploading files to server only with POST method.


See our HTML tutorials to get more knowledge of HTML GET Form and HTML POST Form.

❮ PrevNext ❯