|
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 Retrieve Input Values for Checkboxes Properly?
If multiple input values are submitted with the same field name, like the case of a group of checkboxes,
you should add ([]) to the end of the field name. This tells the PHP engine that multiple values
are expected for this field. The engine will then store the values in an indexed array, and put the array
as the "value" in $_REQUEST. In order to retrieve multiple values of checkboxes properly, you need
to treat $_REQUEST[field_name] as an array. The following PHP script is an enhanced version
of processing_forms.php that handles multiple values properly:
<?php
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++) {
print(" ".$key."[".$i."] = ".$value[$i]."\n");
}
} else {
print(" $key = $value\n");
}
}
print("</pre></html>\n");
?>
Now you need to modify the submit_comments.php as:
<?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>Site Visited:</td><td>"
."<input type=checkbox name=site[] value=dev>Dev FYI Center, "
."<input type=checkbox name=site[] value=sqa>SQA FYI Center, "
."<input type=checkbox name=site[] value=dba>DBA FYI Center "
."</td></tr>\n");
print("<tr><td>Like Site:</td>"
."<td><input type=checkbox name=rate></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 test the form by selecting two checkboxes, you will get something like this:
Number of values: 4
name = Mary
site is an array
site[0] = dev
site[1] = sqa
rate = on
comment = Good sites for developers.
How To Supply Default Values for Text Fields?
If you want to provide a default value to a text field in your form,
you need to pay attention to following notes:
- The default value should be provided in the 'VALUE=default_value' attribute in the <INPUT TYPE=TEXT ...> tag.
- The length of the default value should be less than the max length specified in the "MAXLENGTH=nnn" attribute.
If you provide default value longer than the max length, the default value will be truncated when submitted.
- You should put the default value inside double-quotes as 'VALUE="$default_value"' to protect spaces.
- You must apply htmlspecialchars() translation function to the default value to protect HTML sensitive characters, like
double quotes.
The PHP script below is a modified version of submit_comments.php with a default value in the "comment" field:
<?php
$comment = 'I want to say: "It\'s a good site! :->"';
$comment = htmlspecialchars($comment);
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 value=\"$comment\" size=40>"
."</td></tr>\n");
print("<tr><td colspan=2><input type=submit><td></tr></table>\n");
print("</form></html>\n");
?>
If you view this PHP page, you will a form with default value nicely displayed in the comment field.
If you submit the form, you will get something like this:
Number of values: 2
name = Alan
comment = I want to say: \"It\'s a good site! :->\"
Notice that special characters are protected with slashes when form is submitted. See the next tip
on how to remove slashes.
(Continued on next part...)
Part:
1
2
3
4
5
6
7
8
|