Before you can process the information, you need to create an HTML form that will send information to your PHP script. There are two methods for sending data: POST and GET. These two types of sending information are defined in your HTML form element's method attribute. Also, you must specify the location of the PHP file that will process the information.
Below is an HTML form that will send the data using the POST method. Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send. Copy and paste this code and save it as form.html.
<head>
<title>Process the HTML form data with the POST method</title>
</head>
<body>
<form name="myform" action="process.php" method="POST">
<input type="hidden" name="check_submit" value="1" />
Name: <input type="text" name="Name" /><br />
Password: <input type="password" name="Password" maxlength="10" /><br />
Select something from the list: <select name="Seasons">
<option value="Spring" selected="selected">Spring</option>
<option value="Summer">Summer</option>
<option value="Autumn">Autumn</option>
<option value="Winter">Winter</option>
</select><br /><br />
Choose one:
<input type="radio" name="Country" value="USA" /> USA
<input type="radio" name="Country" value="Canada" /> Canada
<input type="radio" name="Country" value="Other" /> Other
<br />
Choose the colors:
<input type="checkbox" name="Colors[]" value="green" checked="checked" /> Green
<input type="checkbox" name="Colors[]" value="yellow" /> Yellow
<input type="checkbox" name="Colors[]" value="red" /> Red
<input type="checkbox" name="Colors[]" value="gray" /> Gray
<br /><br />
Comments:<br />
<textarea name="Comments" rows="10" cols="60">Enter your comments here</textarea><br />
<input type="submit" />
</form>
</body>
</head>
</html>
The example HTML page above includes different form elements: input fields, select list, text area, radio buttons, checkboxes and a submit button. When a user fills in this form and clicks on the submit button, the form data is sent to the process.php file.
Notice that we have added square brackets [] to the name of the checkbox element. The reason for the square brackets is that it informs PHP that the value may be an array of information. Users can select multiple values, and PHP will place them all into an array of the value of the name attribute.
For example, if you pick Green and Yellow and submit the form, then $_POST['Colors'] is itself an array. $_POST['Colors'][0] is Green and $_POST['Colors'][1] is Yellow. That's because the name attribute of the checkbox element is Colors[]. If the name was just Colors, then $_POST['Colors'] would be a string, holding only one of the selected values.
Processing the Form Data ( PHP Code )
Next, we are going to create our PHP file that will process the data. When you submit your HTML form PHP automatically populates two superglobal arrays, $_GET and $_POST, with all the values sent as GET or POST data, respectively. Therefore, a form input called 'Name' that was sent via POST, would be stored as $_POST['Name'].
Copy and paste this code and save it as process.php in the same directory as form.html.
//Check whether the form has been submitted
if (array_key_exists('check_submit', $_POST)) {
//Converts the new line characters (\n) in the text area into HTML line breaks (the <br /> tag)
$_POST['Comments'] = nl2br($_POST['Comments']);
//Check whether a $_GET['Languages'] is set
if ( isset($_POST['Colors']) ) {
$_POST['Colors'] = implode(', ', $_POST['Colors']); //Converts an array into a single string
}
//Let's now print out the received values in the browser
echo "Your name: {$_POST['Name']}<br />";
echo "Your password: {$_POST['Password']}<br />";
echo "Your favourite season: {$_POST['Seasons']}<br /><br />";
echo "Your comments:<br />{$_POST['Comments']}<br /><br />";
echo "You are from: {$_POST['Country']}<br />";
echo "Colors you chose: {$_POST['Colors']}<br />";
} else {
echo "You can't see this page without submitting the form.";
}
?>
Let's give a little explanation. At the first line we check whether the form has been submitted and the php script has not been called directly. Next we convert the new line characters in the text area into HTML line breaks. Then we check whether a $_POST['Colors'] is set and if so we use implode() function to convert $_POST['Colors'] array into a single string. Finally, we print out all received values in the browser.
<a href="http://www.ny-aca.com">Hearing Aids Newyork</a>
<a href="http://www.ny-aca.com">Hearing Aids Staten island</a>
<a href="http://www.ny-aca.com">Hearing Aids NYC</a>
<a href="http://www.ny-aca.com">Advanced Care Audiology</a>
<a href="http://www.ny-aca.com">Audiology</a>
<a href="http://www.ny-aca.com">hearing aids</a>