Handle a basic HTML form with PHP
The HTML forms are widely used in websites and web applications. They help us to collect information from users and deliver appropriate result from server.
The HTML forms are basic user interfacing. With these HTML forms, we want to use some server-side scripting language to talk with servers. The PHP is a powerful server-side scripting language & it is widely used in web applications.
Through this post, we are making a basic HTML form, collect some data from our user & deliver appropriate result from server with the help of PHP.
Step 1: Create a HTML form
To talk with server, now we are making a basic HTML forms. They have two text fields & a submit button. When we press the submit button, our values in text field are submitted in to
welcome.php page in server & it display a welcome screen.
<html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> E-mail: <input type="text" name="email"><br> <input type="submit" value="submit"> </form> </body> </html>
Our HTML form is very easy to understand. It ask our name & Email address. Then it submit our values in to
step 2: Create a PHP page to welcome our visitors
To process these collected values, we want to create a PHP page. In our HTML form, we already tell that we are going to use
welcome.php as the action of that form.
So now we are going to create a basic PHP page,
welcome.php to welcome our visitors.
<html> <body> Welcome <?php echo $_POST["name"]; ?><br> Your email address is: <?php echo $_POST["email"]; ?> </body> </html>
In our PHP page, we only use basics functions. The
echo is not a PHP function, it is a language construct. To get output from PHP page, we use this
If you want to print
Hello from a PHP page, you just want to
<?php echo "Hello"; ?>. The
<?php is the opening tag &
?> is the closing tag in PHP.
If we want to print the value of a variable, we want to
echo that variable like
<?php echo $my_variable; ?>.
welcome.php page, we
echo two variable,
$_POST is a global variable & the name of our HTML form fields will automatically be the keys in the
In our HTML form, the name of form fields are
$_POST array are
$_POST["key"] and then we display its values using
The output will be a welcome screen that welcome our user with his name & email.
Welcome sarath Your email address is: email@example.com
Anyway, this is a basic form handling in PHP & I have to learn more.
- Redirect single pages of post type in to post type archive page - WordPress
- Get details about available image sizes - WordPress
- Change shipping and billing address title - WooCommerce
- Add phone number to the output of the addresses on the "My Account" page - WooCommerce
- Conditional tags for order thank you page - WooCommerce
Your Questions / Comments
If you found this article interesting, found errors, or just want to discuss about them, please get in touch.