- Form handling is much important task in php. A form has some input fields in which user can enter some relevant data. On pressing submit button, the filled data is passed over the server. Now when you get the data entered by user, you can use it for the required purpose. Form creation is discussed in HTML Form section.
<form action="sample1.php" method="post">
Name: <input type="text" name="username" />
Passing the data
In the form tag, there are two important attributes-
- action= (Required) It defines the name of the file, to which the data will be transferred.
- method= (Optional) It defines the method of passing the data. It may have one of two values-
- get- This is the default value. In this method, the query is generated in the address bar and the data will be visible in url. This can be used for non- secure data.
- post- In this method, the result is appended to the body of HTTP request and the data will not be visible. This is used for secured data.
receiving the data
The data sent by a form can be received through php script.
- To receive the data sent via get method- $var= $_GET['fieldName'];
- To receive the data sent via post mehod- $var=$_POST['fieldName'];
- Here fieldname is the name given to the input field in the html.
Passing data to different file
- In this example, the form is created in a separate html file and the data is passed to some other php file where it will be manipulated accordingly.
- Create a form in sample.html file.
- Source program is converted into object program with the help of interpreter or compiler which are explained here in detail:
<tr><td>Name:</td><td><input type="text" name="name" /></td></tr>
<tr><td>Gender:</td><td>input type="radio" name="gender" value="Male" /> Male</td></tr><br>