PHP Form

In this post I would create a simple form in HTML (with just two input boxes) and then write PHP code to retrieve the form values a user submit using PHP.

So in our sample project there are only two files:
  1. form.html
  2. form-handler.php
Let see the form.html code:



PHP Form




First Name:

Last Name:






This form will generate following output:












There are following points to note:

  1. The action attribute value is form-handler.php. It shows, when the form is submitted form-handler.php would process the form. Processing includes, retrieving form data and storing it somewhere if you want to.
  2. The input text boxes names are first-name and last-name. On PHP page, we would use these  names to retrieve the values user entered in the PHP form.

The Server Side

The form-handler.php code retrieves the both input values and display on the next page. Lets first look at the form-handler.php code.


$first_name = $_REQUEST['first-name'];
$last_name = $_REQUEST['last-name']; ?>
echo "


Form Handler



First Name : $first_name
Last Name: $last_name


";

Please note following points regarding we process the form values at server side:

  1. The PHP receives the submitted values and put them into global associatve array $_REQUEST. We can retrieve values using the input text field names.
  2. In the code above, we have retrieved the values from $_REQUEST array, store them into our local variable and then printed those values inside the HTML BODY element.
When we submit the form, following output is displayed:

Comments

  1. Respected Sir finally I found you. Kindly share how to make Rest API in php and way to use it in my (final year project) ie: Android Application

    ReplyDelete

Post a Comment