Categories:
.NET (961)
C (387)
C++ (185)
CSS (84)
DBA (8)
General (31)
HTML (48)
Java (641)
JavaScript (220)
JSP (109)
JUnit (31)
MySQL (297)
Networking (10)
Oracle (562)
Perl (48)
Perl (9)
PHP (259)
PL/SQL (140)
RSS (51)
Software QA (28)
SQL Server (5)
Struts (20)
Unix (2)
Windows (3)
XHTML (199)
XML (59)
Other Resources:
How To Retrieve Input Values for Checkboxes Properly
How To Retrieve Input Values for Checkboxes Properly? - PHP Script Tips - Processing Web Forms
✍: FYIcenter.com
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.
2016-06-26, 12257👍, 1💬
Popular Posts:
Are risk constant through out the project ? * Never say that risk is high through out the project. R...
.NET INTERVIEW QUESTIONS - Did VB6 support multi-threading ? While VB6 supports multiple single-thre...
Do events have return type ? No, events do not have return type.
How To Empty Your Recycle Bin? - Oracle DBA FAQ - Managing Oracle Database Tables If your recycle bi...
How To Define a Sub Function? - Oracle DBA FAQ - Creating Your Own PL/SQL Procedures and Functions A...