PHP and HTML Forms

PHP is the scripting language that helps in creating impressive and interactive web applications. So you need HTML forms for collecting data from users, processing it and saving it into database. The saved data can be fetched from backend and displayed in the webpage. PHP and HTML forms are used to create web applications in following type of activities

  • Use HTML form elements to make user interaction easy and intuitive
  • Accept data from a user in an HTML form and pass it on to PHP script to process form  data
  • Validate and save data entered by the user

In this post we will discuss all the elements of HTML forms to create interactive applications.

To connect a web form with PHP scripts on server, the web form must be created with HTML form controls. HTML tag FORM is used to enclose all the HTML forms elements that are presented to the users for filling the data and submitting it. The HTML FORM tag is used in the following way

<FORM>
Form controls using HTML form tags
</FORM>

Using PHP and HTML Forms

The HTML form must know what to do when user submits the data. HTML form has two attributes for this- method and action. They must be added in the FORM tag.

Method– This FORM attribute is given one of the two methods- get and post. These methods take the data to the PHP scripts defined to handle the data.

Action – This attribute takes the name of the script where the data is to be sent by get or post method. You can read here to understand meaning and difference between GET and POST. The target PHP script is executed on form submitting. It is added as a url or its name for action attribute’s value. If action attribute value is a name of the script, its absolute or relative location must also be given.

For example

<FORM method="post" action="https://csveda.com/uservalidate.php">
</FORM>

The form element will send the user data to the url given in action attribute value.

<FORM method="post" action="uservalidate.php">
</FORM>

The form element will send the user data to the script specified in action attribute by using the relative path. If action attribute has script name without any path, it must be in the same folder where the sending HTML form is stored.

You can store all your PHP script files in a folder may be called “scripts” under folder storing all the HTML forms. You can use path in action attribute like-

<FORM method=”post” action=”/scripts/uservalidate.php”>

Example

login.html

<HTML>
<BODY>
<FORM method="post" action="uservalidate.php">
<DIV style="border: 2px solid darkblue;padding:10px; align:center;width:50%">
User Name:<input type="text" name="user">
Password:<input type="password"  name="pswd">
<input type="submit" value="SUBMIT">
</DIV>
</FORM>
</BODY>
</HTML>
Input form for user login

uservalidate.php

<?php
$user=$_POST['user'];
$pwd=$_POST['pswd'];
echo( '<h1>Hello '. $user. '! Your password is '.$pwd);
?>
Output after execution of PHP script

Sending Data to same URL which Sends it to a PHP script

This is a very interesting thing that you can do in a webpage. If you want to send the data from a webpage to itself then omit the action attribute altogether. Such a webpage have to be a script file (.php). To design interface and handle the sent data you will need script in PHP and HTML forms elements in same webpage file.  The form tag will look like this.

<FORM method=”post”>

Example

<HTML>
<BODY>
<FORM method="post" >
<!--form created-->
<DIV style="border: 2px solid darkblue;padding:10px; align:center;width:50%">
User Name:<input type="text" name="user">
Password:<input type="password"  name="pswd">
<input type="submit" value="SUBMIT" name="submit">
</DIV>
</FORM>
</BODY>
</HTML>
<?php
// check if the form is submitted
if (isset($_POST['submit']))
{
	// save sent values from form in variables
	$user=$_POST['user'];
	$pwd=$_POST['pswd'];
	echo( '<h1>Hello '. $user. '! Your password is '.$pwd);
}
?>
PHP and HTML forms return submitted values to form that sends data to PHP script

So, this is the way you can work with HTML Form controls and PHP. Let’s begin to understand how to use other HTML form controls in Web Applications with PHP.

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *