Php form example

In this tutorial, we will send data from simple html form and receive form data on other page with the help of php.

First of all make form as given in example.

<form name="form1" action="simple_fom_result.php" method="post">
<tr><td>First Name</td><td><input type="text" name="first_name" id="first_name" /></td></tr>
<tr><td>Last Name</td><td><input type="text" name="last_name" id="last_name" /></td></tr>
<tr><td>Gender </td> <td> <input type="radio" name="gender" id="gernder-1" value="male" checked="checked" /> Male
<input type="radio" name="gender" id="gernder-2" value="female" /> Female
<tr><td>Stream </td><td>
<select name="stream" id="stream" >
<option value="commerce">Commerce</option>
<option value="science">Science</option>
<option value="computer">Computer</option>
<tr><td>Your Hobbies </td><td>
<textarea name="hobbies" id="hobbies" rows="5" cols="36"></textarea>
<tr><td></td><td><input type="submit" name="btn" id="btn" value="Submit" /></td></tr>

As we see , there are three attributes of html form are set i.e. name, action and method. The name attribute is used for form's name, action attribute is for where we want to get data after clicking submit button.The third attribute is method, There two methods for sending data i.e. get and post. If we don't set any method, by default get method is set.And if get method is set, the all values of form are shown in url when we submit data, but post method does not show values in url.

The next step is making php page for receiving data of form. As in above example, our data receiving page name is simple_fom_result.php. So make page with name simple_fom_result.php and write coding as given below:


echo "First name : ".$_POST["first_name"]."<br/>";
echo "Last name : ".$_POST["last_name"]."<br/>";
echo "Gender : ".$_POST["gender"]."<br/>";
echo "Stream : ".$_POST["stream"]."<br/>";
echo "Hobbies : ".$_POST["hobbies"]."<br/>";
<a href="simple_form.php">Back</a>

In php two arrays are used to receive the form data $_POST / $_GET. $_POST is used if data is sent by post method. If form method is get then $_GET is used. As we see for getting values of form, name of form element is used in index of $_POST i.e. $_POST['name_of_form_element'].