DONATE Hyvor Developer
PHP
PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP StringsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms Finishing

PHP Forms Creating

In PHP Forms tutorial, we will discuss about $_GET and $_POST superglobal variables. First, you need to understand that the value of every form element is sent with its name (key/value pair) when submitting the form.

First HTML Form

The following example will create a simple HTML form with three input fields and submit button.

PHP First HTML Form


<html>
<head>
	<title>PHP Forms</title>
</head>
<body>

<form method="POST" action="action.php">
	Name: <input type="text" name="name">
	Email: <input type="text" name="email">
	<input type="submit" name="submit">
</form>

</body>
</html>

Run Example ››

When the user hits the submit button after filling the form, data is sent to action.php in POST method. As we learned earlier, data is encoded as key/value pairs. The name of the input field remain as the key.

Creating The PHP Handler

When the form is submitted, the data is sent to a file which handles the data. Here we will create action.php file for that. Our PHP handler file will show a welcome message to the user using the data passed into the file.

  • In the POST method, data is saved in $_POST superglobal.
  • In the GET method, data is saved in $_GET supergloabl.
  • In both methods, you can access data with $_REQUEST. But, it is recommended to use $_POST or $_GET.
  • PHP Form Handler

    
    <!DOCTYPE html>
    <html>
    <head>
    	<title>Welcome <?php echo $_POST['name']; ?></title>
    </head>
    <body>
    
    <p>Hello <?php echo $_POST['name'] ?>,</p>
    <p><?php echo $_POST['email'] ?> is your email.</p>
    
    </body>
    </html>
    
    
    Run Example ››

    Using Get Method

    Get method can be used in the same way. There is only two differences.

    • The method attribute of <form> element is set to "GET".
    • We use $_GET instead of $_POST in the handler.

    HTML Form

    
    <html>
    <head>
    	<title>PHP Forms</title>
    </head>
    <body>
    
    <form method="GET" action="forms-handler-get.php">
    	Name: <input type="text" name="name">
    	Email: <input type="text" name="email">
    	<input type="submit" name="submit">
    </form>
    
    </body>
    </html>
    
    

    PHP Handler

    
    <!DOCTYPE html>
    <html>
    <head>
    	<title>Welcome <?php echo $_GET['name']; ?></title>
    </head>
    <body>
    
    <p>Hello <?php echo $_GET['name'] ?>,</p>
    <p><?php echo $_GET['email'] ?> is your email.</p>
    
    </body>
    </html>
    
    
    Run Example ››

    HTML Form and PHP Handler One File

    You can set the same file to show the html form as well as to handle the request. Setting action attribute to no value will make the browser submit the form to the same page.

    Tip: $_SERVER['REQUEST_METHOD'] holds a string representing the request method.

    HTML Form + PHP Handler

    
    <!DOCTYPE html>
    <html>
    <head>
    	<title>Form</title>
    </head>
    <body>
    
    <?php if ($_SERVER['REQUEST_METHOD'] === 'POST') : ?>
    
    	<p>Hello <?php echo $_POST['name'] ?>,</p>
    	<p><?php echo $_POST['email'] ?> is your email.</p>
    
    <?php else : ?>
    
    	<form method="POST" action="">
    		Name: <input type="text" name="name">
    		Email: <input type="text" name="email">
    		<input type="submit" name="submit">
    	</form>
    	
    <?php endif; ?>
    
    </body>
    </html>
    
    
    Run Example ››

    In case you missed this special if-else with html chapter, you can learn it here.

    Security is the most important part in forms! Continue to the next chapter before creating your own form.
    5
    222