-
PHP scripts are used to process information from a Web form. The information can be sent to an email address, or the data can be used in databases or calculations. The PHP code is embedded in a Web page in a PHP section with the PHP tag <? and ?>. PHP is written in plain text and will be processed by the PHP pre-processor on the server before the HTML that constitutes the Web page is sent to the client who requested the page.
Before writing the PHP code for a Web form, be sure the form is set up correctly and clearly. Design your form with the processing in mind. Each item on the form, no matter how insignificant, needs to have a name that is clear so you can find it in the PHP processing step. Be sure that all the names have no spaces to make processing in PHP easier. To make writing the PHP code easier, print out a copy of your form with the form items on it, and then write the names of each item on the paper so you can quickly and easily reference them when you're writing the PHP code.
The form will need to have a destination of a PHP page. For example, your form tag may look like this:
<form action="process.php" method="post">
You can have a destination of the current PHP page and process the form at the top of the page, if you desire. -
After the form has been submitted to the PHP page, all the form variables will be available to the PHP processor. They will be in an array of the $_POST variable. For example, if you have a text field that has the name "yourname," you can access the value that was typed into that field with the variable $_POST['yourname'].
To avoid problems with spammers and hackers, all data submitted to the form should be processed through htmlspecialchars to throw out simple hacks. This can be done with a simple assignment statement:
$yourname = htmlspecialchars($_POST['yourname']) -
Once the data has been received from the form and processed, that data can be used as needed. The data can be placed in a database using MySQL commands, it can be sent to others using PHP email functions, or it can be used in any other manner. If the form's action is the same page, code can be added at the top of the page to check to see if the form has just been submitted using code like this:
if ($_POST['nameOfFormField'] > 0) { // processing code here }
This will allow you to submit the form, process the data and then show the user the same page for things like working with databases and adding and removing records.












