|
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
(Continued from previous part...)
How To Support Hidden Form Fields?
Hidden fields are special fields in a form that are not shown on the Web page.
But when the form is submitted, values specified in the hidden fields are also submitted
to the Web server. A hidden field can be specified with the <INPUT TYPE=HIDDEN ...> tag.
The PHP script below shows you a good example:
<?php
print("<html><form action=processing_forms.php method=post>");
print("<input type=hidden name=module value=FAQ>\n");
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 size=40>"
."</td></tr>\n");
print("<tr><td colspan=2>"
.'<input type=submit name=submit value="Submit">'
."<td></tr></table>\n");
print("</form></html>\n");
?>
If you submit this form, you will get something like this:
Number of values: 4
module = FAQ
name = Peter
comment = Thanks for the good tips.
submit = Submit
How To Generate and Process a Form with the Same Script?
In previous exercises, a Web form is generated by one script, and processed
by another script. But you could write a single script to do both. You just need
to remember to:
- Use same script name as the form generation script in the "action" attribute in the <FORM> tag.
- Write two sections in the script: one for form generation, the other for form processing.
- Check one expected input to determine which section to use.
The PHP script below shows you a good example:
<?php
if (!isset($_REQUEST['submit'])) {
generatingForm();
} else {
processingForm();
}
function generatingForm() {
print("<html><form action=submit_comments.php method=post>");
print("<input type=hidden name=module value=FAQ>\n");
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 size=40>"
."</td></tr>\n");
print("<tr><td colspan=2>"
.'<input type=submit name=submit value="Submit">'
."<td></tr></table>\n");
print("</form></html>\n");
}
function processingForm() {
print("<html><pre>");
$count = count($_REQUEST);
print("Number of values: $count\n");
foreach ($_REQUEST as $key=>$value) {
if (is_array($value)) {
print(" $key is an array\n");
for ($i = 0; $i < count($value); $i++) {
$sub_value = $value[$i];
if (get_magic_quotes_gpc()) {
$sub_value = stripslashes($sub_value);
}
print(" ".$key."[".$i."] = ".$sub_value."\n");
}
} else {
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
print(" $key = $value\n");
}
}
print("</pre></html>\n");
}
?>
If you save this script as submit_comments.php on your Web server, and
submit this form, you will get something like this:
Number of values: 4
module = FAQ
name = Ray
comment = Good site for beginners.
submit = Submit
(Continued on next part...)
Part:
1
2
3
4
5
6
7
8
|