Click to See Complete Forum and Search --> : Problems with forms


Anon
07-10-2001, 02:55 AM
Hello, let's see if someone can help me.
A have created an HTML form like this:
<FORM NAME="formulario" ACTION="prueba.php" METHOD="POST">
1<INPUT TYPE="checkbox" NAME="valor" VALUE="1"><BR>
2<INPUT TYPE="checkbox" NAME="valor" VALUE="2"><BR>
<INPUT TYPE="submit" VALUE="Enviar">
</FORM>
How could I get (with PHP) the values "1" and "2" of $valor in the case both values are checked in the form? The problem is that if I did (in the INPUTs) NAME="valor[]" I could catch them as $valor[0] and $valor[1], but then I use and document.formulario.valor doesn´t recognize it as an object (it gives me a runtime error). Thank you very much.

dstockto
07-10-2001, 12:47 PM
Try this

<?php
if (isset($valor))
{
$num_elements=count($valor);
for ($i=0; $i < $num_elements; $i++)
{
echo ("Number " . $valor[$i] . " was selected.<BR>\n");
}
}
?>

This does mean you have to use valor[] as the variable name though.

Try referencing document.formulario.valor%5B%5D

This might work for what you want to do, but I am not sure. %5B and %5D are the [ and ] characters.

Hope this helps.

Dave

Anon
07-11-2001, 12:45 AM
Tahnk you for your reply. Unfortunately, neither of the proposed solutions work. I'm bored of JavaScript!! However, don't worry, perhaps I can solve it in any way.

Anon
07-11-2001, 01:14 PM
Hi David,

The variable $valor must be $valor[] as David Stockton already wrote.

To get the selected values out you could use code like :

while(list($key, $sel) = each($valor)) {
echo "<li>$sel\n";
}