PHP Form

There are medium PHP form for taking data input from the user on any web page.

The user fills the required information with the help of Forms and then clicks on a button. When the user click on the submit button, the action is performe. This action defines the action attribute of the <form> tag.

In this attribute, you either address an HTML file or give a scripting file such as the address of PHP or JavaScript.
If you want to handle HTML form through PHP then you name a PHP file in the action attribute, and in that PHP file, you handle the form.

Example

index_form.html

<html>
<head>
<title>HTML Form</title>
</head>
<body>
<form action=”action_file.php” method=”GET”>
Enter Name : <input type=”text” name=”userName”><br>
Enter Age : <input type=”text” name=”Age”>
<input type=”submit”>
</form>

</body>
</html>

action_file.php

<?php
// Getting name
$username = $_GET[‘userName’];

// Getting age
$age = $_GET[‘Age’];

// Printing variable values
echo “Name”,$username;
echo “Age”,$age;

?>

Output

Name Jhon
Age 25

In the above example, a form has been created in the index_form.html file. action_file.php file has been defined as action in this form. Also a method has been defined which is GET. You will learn more about this method.

The action_file.php is accessed from the form via the GET array in PHP file. Now either this information can be printed in the PHP file back browser (as is done in the example) or store it in the database.

The PHP code is on the server and the HTML code is in the browser on the client side. You have two methods to send information to Browser. You can do this with the help of GET and POST methods.

$_GET

$ _GET is an array. If the method is GET then all the values that are submitted by the user can be accessed by this array. Just in this you have to pass the name of that particular form element. And it returns you to its value.

  • Sends the GET method form by encoding it to the information URL. Anyone can easily read this information. Therefore, GET method is not considere safe. If the user is submitting any sensitive information then you should never use the GET method.
  • With GET method you can send information of very few characters.
  • You can not send media via the GET method. For example, someone like you is uploading your image, in such a situation you can not use the GET method.
  • GET is sent in the form of information array in which the name of the tag works as an index and the value of the tag is the value of that index. Like there is an array of $ _GET.

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Example of PHP GET method</title>
</head>
<body>
<?php
if(isset($_GET["name"])){
    echo "<p>Hi, " . $_GET["name"] . "</p>";
}
?>
<form method="get" action="<?php echo $_SERVER["PHP_SELF"];?>">
    <label for="inputName">Name:</label>
    <input type="text" name="name" id="inputName">
    <input type="submit" value="Submit">
</form>
</body>

$_POST

The $_POST method is secure instead of the $_GET method. In this method, you send information via HTTP headers. You should always use $_POST method for sensitivity information. Let’s try to know some important points related to the $_POST method.

  • There is no limit to the data in the $_POST method, no matter how much information you can send in it.
  • You can also send media files via $_POST method, such as images and video etc.
  • In the $_POST method all data is pass through the HTTP protocol. That’s why the data is safe.
  • When you use the $_POST method, your information URL does not show up.

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Example of PHP POST method</title>
</head>
<body>
<?php
if(isset($_POST["name"])){
    echo "<p>Hi, " . $_POST["name"] . "</p>";
}
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
    <label for="inputName">Name:</label>
    <input type="text" name="name" id="inputName">
    <input type="submit" value="Submit">
</form>
</body>