Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index

PHP Script Tips - Processing Web Forms

By: FYICenter.com

Part:   1   2  3  4  5  6  7  8 

A collection of 21 tips on process Web forms with PHP scripts. Clear explanations and tutorial exercises are provided on generating and processing Web forms, getting values out of $_REQUEST, processing multiple choices, removing slashes, adding multiple submit buttons, adding hidden values, processing original query string. Topics included in this collections:

  1. How To Create a Web Form?
  2. What Are Form Input HTML Tags?
  3. How To Generate a Form?
  4. Where Is the Submitted Form Data Stored?
  5. How To Retrieve the Submitted Form Data?
  6. What Happens If an Expected Input Field Was Not Submitted?
  7. How To Avoid the Undefined Index Error?
  8. How To List All Values of Submitted Fields?
  9. What Are the Input Values of SELECT Tags?
  10. How To Specify Input Values for Radio Buttons?
  11. How To Specify Input Values for Checkboxes?
  12. How To Retrieve Input Values for Checkboxes Properly?
  13. How To Supply Default Values for Text Fields?
  14. How To Remove Slashes on Submitted Input Values?
  15. How To Support Multiple Submit Buttons?
  16. How To Support Hidden Form Fields?
  17. How To Generate and Process a Form with the Same Script?
  18. How To Submit Values without a Form?
  19. How To Retrieve the Original Query String?
  20. How To Protect Special Characters in Query String?
  21. How To Support Multiple-Page Forms?

How To Create a Web Form?

If you take input data from visitors on your Web site, you can create a Web form with input fields to allow visitors to fill in data and submit the data to your server for processing. A Web form can be created with the <FORM> tag with some input tags. The &FORM tag should be written in the following format:

<form action=processing.php method=get/post>
......
</form>

Where "processing.php" specifies the PHP page that processes the submitted data in the form.

What Are Form Input HTML Tags?

HTML tags that can be used in a form to collect input data are:

  • <SUBMIT ...> - Displayed as a button allow users to submit the form.
  • <INPUT TYPE=TEXT ...> - Displayed as an input field to take an input string.
  • <INPUT TYPE=RADIO ...> - Displayed as a radio button to take an input flag.
  • <INPUT TYPE=CHECKBOX ...> - Displayed as a checkbox button to take an input flag.
  • <SELECT ...> - Displayed as a dropdown list to take input selection.
  • <TEXTAREA ...> - Displayed as an input area to take a large amount of input text.

How To Generate a Form?

Generating a form seems to be easy. You can use PHP output statements to generate the required <FORM> tag and other input tags. But you should consider to organized your input fields in a table to make your form looks good on the screen. The PHP script below shows you a good example of HTML forms:

<?php
  print("<html><form action=processing_forms.php method=post>");
  print("<table><tr><td colspan=2>Please enter and submit your"
    ." comments about FYICenter.com:</td></tr>");
  print("<tr><td>Your Name:</td>"
    ."<td><input type=text name=name></td></tr>\n");
  print("<tr><td>Comments:</td>"
    ."<td><input type=text name=comment></td></tr>\n");
  print("<tr><td colspan=2><input type=submit><td></tr></table>\n");
  print("</form></html>\n");
?>

If you save this script as a PHP page, submit_comments.php, on your Web site, and view this page, you will see a simple Web form.

Where Is the Submitted Form Data Stored?

When a user submit a form on your Web server, user entered data will be transferred to the PHP engine, which will make the submitted data available to your PHP script for processing in pre-defined arrays:

  • $_GET - An associate array that store form data submitted with the GET method.
  • $_POST - An associate array that store form data submitted with the POST method.
  • $_REQUEST - An associate array that store form data submitted with either GET or POST method. $_REQUEST also contains the cookie values received back from the browser.

How To Retrieve the Submitted Form Data?

The best way to retrieve the form data submitted by your visitor is to use the $_REQUEST array. The keys in this array will be the field names defined in form. The values in this array will be the values entered by your visitor in the input fields. The PHP script below, processing_forms.php, shows you how to retrieve form data submitted with the PHP page presented in the previous tutorial exercise:

<?php
  $name = $_REQUEST['name'];
  $comment = $_REQUEST['comment'];
  print("<html><pre>");
  print("You have submitted the following information:\n");
  print("  Name = $name\n");
  print("  Comments = $comment\n");
  print("Thank you!\n");
  print("</pre></html>\n");
?>

If you copy both scripts, submit_comments.php and processing_forms.php, to your Web server, and submit some data like: "Bill Bush" and "Nice site.", you will get something like:

You have submitted the following information:
  Name = Bill Bush
  Comments = Nice site.
Thank you!

(Continued on next part...)

Part:   1   2  3  4  5  6  7  8 


Selected Developer Jobs:

More...