problem with checkboxes in javascript and PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mahmood
Forum Newbie
Posts: 8
Joined: Fri Mar 19, 2004 5:23 pm

problem with checkboxes in javascript and PHP

Post by mahmood »

Summary: when i create a series of checkboxes using php, I cannot check if there is at least one checked or not.

and full story: in a script like bellow:
<?
echo "<form name=myform method=post action=test2.php>";
for ($i=0; $i<10; $i++)
{
echo "<input type=checkbox name=mycheckbox[] value=$i><br>";
}
echo "<input type=button onclick='myfunction()'>";
echo "</form>";
?>
if I dont' put [] in front of name attribute i wont be able to send values to the other page (test2.php). and if i put square brackets i cannot find the length of check boxes in my javascript function.

so how do I check if there is at least one checkbox is checked before submitting the form?
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: problem with checkboxes in javascript and PHP

Post by TheBentinel.com »

mahmood wrote:if I dont' put [] in front of name attribute i wont be able to send values to the other page (test2.php). and if i put square brackets i cannot find the length of check boxes in my javascript function.
use document.form["formName"]["checkboxName[]"][0].checked

or maybe it's value, but you get the idea. use the name in quotes surrounded by brackets to reference the items in your form, rather than the doc.form.field notation.

Hope it helps!
Post Reply