Friday, March 12, 2010

11:40 PM
One of the best features of PHP is possibility to respond to user queries or data submitted from HTML forms. You can process information gathered by an HTML form and use PHP code to make decisions based off this information to create dynamic web pages. In this tutorial we will show how to create an HTML form and process the data.

The HTML Form
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.
<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.

0 comments: