note 46952 deleted from language.variables.external by danbrown
| From: | danbrown@php.net | Date: | Wed, 09 Mar 2011 14:40:31 +0000 |
| Subject: | note 46952 deleted from language.variables.external by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177706@lists.php.net to get a copy of this message | ||
Note Submitter: pascal dot poncet at netconsult dot com
----
Concerning the use of both javascript and php in working with forms :
In a php code, it's quiet easy and practical to use names like "fieldname[]" in
checkbox input type html elements. You will then retrieve the value(s) transmitted in the
"fieldname" array type variable.
But a difficulty appears in coding some javascript function able to manipulate those fields values
to verify them, as "fieldname[]" must not be recognize directly as an array in the script.
Here is a nice way to solve this question :
(html code)
<form method="post" action="..." onsubmit="return
verifyForm(this)">
<input type="checkbox" name="colors[]" value="black">
black<br>
<input type="checkbox" name="colors[]" value="white">
white<br>
<input type="checkbox" name="colors[]" value="other"> other
color<br>
</form>
(javascript code)
function verifyForm(form) {
var selColors=new String();
for(i=0;i<form["colors[]"].length;i++)
if(form["colors[]"][i].checked) selColors+=form["colors[]"][i].value;
if(selColors=="") msgErr="You have to check at least one choice of color";
...
}
Of course, you may also verify anything else about the checked values, like the value itself, only
by using 'form["colors[]"][i].value' instead of '...checked'.
Happy to help you,
Pascal
PS : apologying for my bad english.