validate a form and choosing a link

P

Pascal

hello i am new to javascript and I am trying to send the visitor of my page
to another depending what he feeds in the form but there is an error i can't
figure ! Can you help me to find it please?

the error says line 27 carac 3 voc is undefined and if I put 0 instead of
voc, it works ! Why ? The value of this radio button is voc ! not 0

thanks
pascal

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<script type="text/javascript">

//<![CDATA[
<!--
function verif_formulaire()
{
// validate matiere
myOption = -1;
for (i=formulaire.matiere.length-1; i > -1; i--) {
if (formulaire.matiere.checked) {
myOption = i; i = -1;
}
}
if (myOption == -1) {
alert("Tu dois choisir une matière");
return false;
}

//Choix de la page
if(document.formulaire.matiere[voc].checked == true)
{
document.formulaire.action ="voc/index.php";
}
else
if(document.formulaire.matiere[conj].checked == true)
{
document.formulaire.action ="conj/index.php";
}
else
if(document.formulaire.matiere[gram].checked == true)
{
document.formulaire.action ="gram/index.php";
}
else
if(document.formulaire.matiere[orth].checked == true)
{
document.formulaire.action ="orth/index.php";
}
else
if(document.formulaire.matiere[lect].checked == true)
{
document.formulaire.action ="lect/index.php";
}
return true;

}
</script>
<form name="formulaire" onsubmit="return verif_formulaire()" id="formulaire"
method="post" enctype="multipart/form-data">
<fieldset><legend>ETAPE N°2</legend>
Choisir la matière.<br />

<input id="voc" name="matiere" type="radio" value="voc" />Vocabulaire
<input id="conj" name="matiere" type="radio" value="conj" />Conjugaison
<input id="gram" name="matiere" type="radio" value="gram" />Grammaire
<input id="orth" name="matiere" type="radio" value="orth" />Orthographe
<input id="lect" name="matiere" type="radio" value="lect" />Lecture

</fieldset>
<input type="submit" value="S'inscrire">
</form>
</body>
</html>
 
J

Jonathan Fine

Pascal said:
hello i am new to javascript and I am trying to send the visitor of my
page to another depending what he feeds in the form but there is an
error i can't figure ! Can you help me to find it please?

the error says line 27 carac 3 voc is undefined and if I put 0 instead
of voc, it works ! Why ? The value of this radio button is voc ! not 0

In
object[aaa] = bbb
the key is aaa and the value is bbb. I suggest that the key is indead 0.

You might want to replace your if .. else ... by a loop or a switch
statement.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<script type="text/javascript">

//<![CDATA[
<!--
function verif_formulaire()
{
// validate matiere
myOption = -1;
for (i=formulaire.matiere.length-1; i > -1; i--) {
if (formulaire.matiere.checked) {
myOption = i; i = -1;
}
}
if (myOption == -1) {
alert("Tu dois choisir une matière");
return false;
}

//Choix de la page
if(document.formulaire.matiere[voc].checked == true)
{
document.formulaire.action ="voc/index.php";
}
else
if(document.formulaire.matiere[conj].checked == true)
{
document.formulaire.action ="conj/index.php";
}
else
if(document.formulaire.matiere[gram].checked == true)
{
document.formulaire.action ="gram/index.php";
}
else
if(document.formulaire.matiere[orth].checked == true)
{
document.formulaire.action ="orth/index.php";
}
else
if(document.formulaire.matiere[lect].checked == true)
{
document.formulaire.action ="lect/index.php";
}
return true;

}
</script>
<form name="formulaire" onsubmit="return verif_formulaire()"
id="formulaire" method="post" enctype="multipart/form-data">
<fieldset><legend>ETAPE N°2</legend>
Choisir la matière.<br />

<input id="voc" name="matiere" type="radio" value="voc" />Vocabulaire
<input id="conj" name="matiere" type="radio" value="conj" />Conjugaison
<input id="gram" name="matiere" type="radio" value="gram" />Grammaire
<input id="orth" name="matiere" type="radio" value="orth" />Orthographe
<input id="lect" name="matiere" type="radio" value="lect" />Lecture

</fieldset>
<input type="submit" value="S'inscrire">
</form>
</body>
</html>
 
M

Matthias Reuter

the error says line 27 carac 3 voc is undefined and if I put 0 instead
of voc, it works ! Why ? The value of this radio button is voc ! not 0
if(document.formulaire.matiere[voc].checked == true)

You need to put quotes around voc, since you literally mean voc. The way
you wrote it means "the value of the variable voc", which does not exist.
And you can omit the comparison to true.

if (document.formulaire.matiere["voc"].checked)

Your code can be optimized further, but that's more than you asked for :)

Matt
 
R

Richard Cornford

Pascal said:
hello i am new to javascript and I am trying to send the visitor
of my page to another depending what he feeds in the form but
there is an error i can't figure ! Can you help me to find it
please?
the error says line 27 carac 3 voc is undefined and if I put 0
instead of voc, it works ! Why ? The value of this radio button
is voc ! not 0

In
object[aaa] = bbb
the key is aaa and the value is bbb.

In a bracket notation property accessor the name of the property is
determined by evaluating the expression within the square brackets and
then type-converting the result into a string to give the property
name (the thing you are unhelpfully referring to as "the key"). In
both the original code and your example the expression in the brackets
is an Identifier, which means a scope chain lookup of the Identifier
to retrieve its value. In the event of the Identifier having never
been declared as a variable, function's formal parameter or function
name an error will result from the attempt to retrieve its value.

What you (and the OP) want inside the square brackets is an expression
that will evaluate to a value that either is a string in which the
character sequence corresponds with the desired property name (your
"the key"), or will type-convert into that string. The simplest such
expression is a string literal; - object["aaa"] - in your case and -
document.formulaire.matiere["voc"] - in the OP's.
I suggest that the key is indead 0.
<snip>

Yes, <INPUT type="radio"> sets are not indexable by their VALUE
attributes, only by integer index, which is why - 0 - (the index of
the first radio button in the set) works and, even without the use of
the undeclared Identifier - voc -, the named property access would
not.

Richard.
 
P

Pascal

Hello
Thanks to everybody for your explanation, yes it works by using 0 or 1
etc...
Does select case exist in javascript like in vb ?
pascal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,733
Latest member
LonaMonzon

Latest Threads

Top