note 46952 added to language.variables.external

From: Date: Thu, 28 Oct 2004 15:33:29 +0000
Subject: note 46952 added to language.variables.external
Groups: php.notes 
Request: Send a blank email to php-notes+get-79267@lists.php.net to get a copy of this message
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. ---- Manual Page -- http://www.php.net/manual/en/language.variables.external.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46952 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46952&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46952&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46952&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46952&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46952&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46952&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+46952&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#79267) next »