PDA

View Full Version : variable lan is always zero.?.. ahhhhhh....


havey
04-28-2003, 04:07 PM
I have some radio buttons in a form with the following names, e1,e2,e3,f1,f2, the user can select their langauage ability in both languages, however, i need only the summed value highest scoring language set (either english or french).

to simplify things it would look like this:

English---read---write---speak
excellent-e1-----e2------e3
well-------e1-----e2------e3
poor------e1-----e2------e3

French-----read-----write-----speak
excellent----f1-------f2--------f3
well----------f1-------f2--------f3
poor---------f1-------f2--------f3

So i'm basically trying to add up the English values and then the French and see if the english selections are greateer than the french or vice versa, then i'm transposing this larger language set to a variable (lan)

The following only comes up with lan = 0, its like the code has the correct syntax but the radio values are not being used to calculate the if then statement at the end, why does a1 & a2 variable have no value and var lan always = 0?

function Process () {
var itemchecked = false;

var a1 = a2 = lan = 0;

fr1 = document.f8;
x=1;
while(eval("fr1.e"+x)){
if(eval("fr1.e"+x+".checked")){
a1+=parseInt(eval("fr1.e"+x+".value"));
}
x++;
}

fr2 = document.f8;
y=1;
while(eval("fr2.f"+y)){
if(eval("fr2.f"+y+".checked")){
a2+=parseInt(eval("fr2.f"+y+".value"));
}
y++;
}

if (a1 > a2) {
lan = a1;
}else{
if (a2 > a1) {
lan = a2;
}else{
if (a2 = a1) {
lan = a1;
}
}
}

}

cheesebagpipe
04-28-2003, 04:18 PM
Your script, my friend, is very, very, eval.

Could you post the HTML as well? Hard to test alternatives w/o the form.

beetle
04-28-2003, 04:19 PM
Oog. Too much eval(). I found one error, but cleaned up the code some. Lemme know if this works.function Process()
{
var a1 = a2 = lan = 0;

var f = document.f8;
var x = 1;
var elem

while( elem = f.elements["e"+(x++)] )
{
if( elem.checked )
{
a1 += parseInt( elem.value, 10 );
}
}

x = 1;
while( elem = f.elements["f"+(x++)] )
{
if( elem.checked )
{
a2 += parseInt( elem.value, 10 );
}
}

lan = ( a1 >= a2 ) ? a1 : a2;
}

havey
04-28-2003, 06:59 PM
beetle and cheesebagpipe, thanks for your efforts, i've tried all suggestions, below is the page, You'll have to create a blank page called factor9.htm as the code below posts to it, after submit you should see the lan value in the url: (thanks again)


removed

havey
04-28-2003, 08:48 PM
I ve been playing around with it, trying to restructure it and nothing seem to work, JS is not one of my strong points, any suggestions....

here is the code stripped down to the bare bones:

<html>

<head>


<script type="text/javascript"><!--

function Process()
{
var a1 = a2 = lan = 0;

var f = document.f8;
var x = 1;
var elem

while( elem = f.elements["e"+(x++)] )
{
if( elem.checked )
{
a1 += parseInt( elem.value, 9 );
}
}

x = 1;
while( elem = f.elements["f"+(x++)] )
{
if( elem.checked )
{
a2 += parseInt( elem.value, 9 );
}
}

lan = ( a1 >= a2 ) ? a1 : a2;

document.write (lan)
}

//--></script>

</head>

<body>

<form name="f8">
<p>English</p>
<p>e1<input type="radio" value="10" name="e1">e2<input type="radio" name="e2" value="10">e3<input type="radio" name="e3" value="10"></p>
<p>e1<input type="radio" name="e1" value="5">e2<input type="radio" value="5" name="e2">e3<input type="radio" name="e3" value="5"></p>
<p>e1<input type="radio" name="e1" value="2">e2<input type="radio" name="e2" value="2">e3<input type="radio" value="2" name="e3"></p>
<p>French</p>
<p>f1<input type="radio" value="10" name="f1">f2<input type="radio" name="f2" value="10">f3<input type="radio" name="f3" value="10"></p>
<p>f1<input type="radio" name="f1" value="5">f2<input type="radio" value="5" name="f2">f3<input type="radio" name="f3" value="5"></p>
<p>f1<input type="radio" name="f1" value="1">f2<input type="radio" name="f2" value="1">f3<input type="radio" value="1" name="f3"></p>
<p><input type="Submit" value="Next" onclick="Process();"/></p>
</form>

</body>

</html>

beetle
04-28-2003, 09:04 PM
I see the problem. Each retrieved element is a radio button group, which must be treated like an array.function Process()
{
var a1 = a2 = lan = i = 0;

var f = document.f8;
var x = 1;
var elem

while( elem = f.elements["e"+(x++)] )
{
while ( !elem[i].checked )
{
i++;
}
if ( elem[i].checked )
{
a1 += parseInt( elem[i].value, 10 );
}
}

x = 1;
i = 0;
while( elem = f.elements["f"+(x++)] )
{
while ( !elem[i].checked )
{
i++;
}
if ( elem[i].checked )
{
a1 += parseInt( elem[i].value, 10 );
}
}

lan = ( a1 >= a2 ) ? a1 : a2;

alert( lan );

var sStr = document.location.search;
var val = parseInt(sStr.substring(sStr.indexOf("=")+1,sStr.indexOf(",")));
var sum = (+lan)+((!isNaN(val))?val:0);
data = sStr.substring(sStr.indexOf(","),sStr.length)+ ",(lan="+(+lan)+")";
f.action="factor9.htm?lan="+sum+data ;
//return true;
}