PHP Tutorial - How to read user inputs

In this tutorial you are going to see how to read user inputs using PHP via an HTML form submit. There are three PHP variables are used to read user inputs, they are

1.    $_GET
2.    $_POST
3.    $_REQUEST
Let see these variables in detail

1. $_GET

It is used to read form elements if form method is set to  "GET".

2. $_POST

It is used to read form elements if form method is "POST".

3. $_REQUEST

It is used to retrieve form elements irrespective of form method attribute. That is whether form method is GET or POST it will retrieve form elements. I will recommend using $_REQUEST for safe programming.

Example
  
First let us create an HTML page containing simple form elements.

Sample_form.html

<html>
<head>
<title>Sample html form</title>
</head>
<body>
<form name="form1" action="Sample_display.php">
Enter your name <input type="text" name="user">
<input type="submit" value="submit">
</form>
</body>
</html>

Create an PHP program to read user entered value .

Sample_display.php


<?

$user_name=$_REQUEST["user"]; 

//user is the text box name in Sample_form.html page. $user_name is an variable.

echo "Welcome  ".$user_name;

?>


Output


Sample_form.html



Sample_display.php


Welcome John smith

On clicking the "Submit" button it will redirect to Sample_display.php and display the content entered in the text box.

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.