Here we would integrate the HTML forms and PHP by creating HTML forms to collect the user input turning that input into variables, and then doing various things with the information that we just collected.
First we create a form in HTML and call the file, just like:
<form action=”Admissions.php” method=post>
Your name is:
<br> <input type=”text” name=”YourName”>
Your Address is:
<br /><input type=”text” name=”Address”>
<input type=”submit” name=”submit” value=”Correct!”>
This is a regular HTML form in which:
In line 7, the Form action of HTML command reads action= “Admissions.php” and directs the browser of which PHP document will process the results of the form. A document will be created named “Admissions.php” which generates the result page.
In line 10, input type=“text” determines that the form element which we want here is “text” or a text box (we could also have a radio button, check box, etc.), name=“YourName” determines that whatever the user types into the text box will become a variable that is coded as "YourName." This is what ties together forms and variables - each form field can set a variable to be used however you want.
Similar in line 13, you there is have another text input and name that will recall the address of the applicant to fill in.
Line 16, 17: This code makes a submit button with the text “Correct” and ends the form.
So this form will collect the variable user's name and address.
3.6.1. The $_GET Function
The inbuilt $_GET function is used for collecting values from a form sent with method= “get”. The advantage of using GET function is that it is visible to everyone and can be seen on the URL. It can also be bookmark for further used. For example:
<form action=“Admission.php” method=“get”>
Name: <input type=“text” name=“fname”/>
Age: <input type= “text”name=“age” />
When the user clicks on “Submit” button, the URL sent to the server could look something like this:
Now the $_GET function can be used to collect form data like this:
Admission <?php echo $_GET["fname"]; ?>!<br />
You are <?php echo $_GET[“age”]; ?> years old!
3.6.2. The $_POST Function
The $_POST Function is also used for collecting values from a form sent with method=“post” but unlike $_GET function, the built-in $_POST function is invisible for the end users and has a maximum limitation of 8 MB size but it can be enhanced by setting the post_max_size in the php.ini file; e.g.
<form action=“Admission.php” method=“post”>
Name: <input type=“text” name=“fname” />
Age: <input type=“text” name=“age” />
<input type=“submit” />
When the user clicks the “Submit” button, the URL will look like this:
Now, using $_POST function, the data can be collected from collect form field.
Admission <?php echo $_POST[“fname”]; ?>!<br />
You are <?php echo $_POST[“age”]; ?> years old.
3.6.3. The PHP $_REQUEST Function
The inbuilt $_REQUEST function contains the contents of both $_GET, $_POST, and $_COOKIE, so it works for collecting form data sent with both the GET and POST methods. For Example:
Admission <?php echo $_REQUEST[“fname”]; ?>!<br />
You are <?php echo $_REQUEST[“age”]; ?> years old.
Recommend the tutorial