javascript thread: need help with passing variables between JS and html...
Instead of just giving you the direct answer, I'd like to give you
some points to think about, and choose whatever makes your life easier.
Since you use cgi, I suppose you make server-side programme too.
So, is it really necessary to stick op2 and op3 in client-side to give
op1
to server ? Couldn't your server receive op2 and op3 and stick them
together?
If you want to do the job in client-side, here're a few points to
look at:
1) your
<script language=3D"JavaScript">
op1=3D"combo(op2, op3)";
</script>
is executed only once when the page was first loaded, but it's useless
because op2 and op3 don't contain values that you want.
2) In order to use your combo() function, you should associate the
function
to some action (ie an event). The best place is onsubmit of your form.
3) You should also check if op2 and op3 contains valid values.
4) You should prepare an input for the form to sent op1.
5) You should give your form a name if you want your script to access
form
elements.
Here is the simplest code. Fill ..... with appropriate stuffs :)
<script ....>
function validform() {
var i2 =3D document.carform.op2.selectedIndex
var i3 =3D document.carform.op3.selectedIndex
if (i2 =3D=3D 0 || i3 =3D=3D 0)
return false
else {
var op2 =3D document.carform.op2.options[i2].value
var op3 =3D document.carform.op3.options[i3].value
document.carform.op1.value =3D op2 + op3
return true
}
</script>
<form name=3Dcarform action=3Dhttp://www.cartserver.com/sc/cart.cgi
method=3Dpost onsubmit=3D"return validform()">
<select name=3Dop2>
....
</select>
<br>
<select name=3Dop3>
...
</select>
<input type=3Dhidden name=3Ditem value=3D".........">
<input type=3Dhidden name=3Dop1>
<input type=3Dimage name=3Dnvadd ....>
</form>
> -----Message d'origine-----
> De : Jeremy Smolik [mailto:webmaster@m...]
> Envoy=C3=A9 : jeudi 5 d=C3=A9cembre 2002 20:36
> =C3=80 : javascript
> Objet : [javascript] need help with passing variables between JS and
> html...
>
>
> is it even possible? it doesnt seem so. here's the gist of
> it...we have
> SEVERAL part numbers for different parts based on year, model, etc,
> whatever. user needs to choose multiple options from drop boxes, and
> whatever values attached to them must be combined, and
> entered into an
> html form for processing to our shopping cart (externally
> hosted). so i
> need to have people pick 2 or so things in the boxes, with values
> like "M07" and "22A" that get passed to a function that
> combines them into
> a single string, "M0722A" and pass that back to the html form
> that will
> send that number to the cart cgi.
>
> heres my weak attempt, yeilding frustration:
>
> <head>...
>
> <script language=3D"JavaScript">
> function combo(str1, str2) {
> var crap=3D(str1+str2);
> return crap;
> }
> </script>
>
> ...</head>
>
> <body>
> all the page stuff blah blah....then:
>
> <form action=3Dhttp://www.cartserver.com/sc/cart.cgi method=3Dpost>
> <TD bgcolor=3D"#C0C0C0" height=3D"43" align=3D"left"
> width=3D"40"> </TD>
> <TD align=3Dleft bgcolor=3D"#C0C0C0" height=3D"43"><FONT
color=3Dblack
> size=3D2><B>'82-'88</B></FONT></TD>
> <TD bgcolor=3D"#C0C0C0" height=3D"43" align=3D"left"><font
size=3D"2">
> <select name=3Dop2>
> <option value=3D"=3Derror=3D You must select a
> year.">Choose:</option>
> <option value=3D"00">1983</option>
> <option value=3D"01">1984</option>
> </select>
> <select name=3Dop3>
> <option value=3D"=3Derror=3D You must select van or
> truck.">Choose:</option>
> <option value=3D"02">Truck</option>
> <option value=3D"03">Van</option>
> </select>
> <script language=3D"JavaScript">
> op1=3D"combo(op2, op3)";
> </script>
> </TD>
> <TD bgColor=3D#C0C0C0 height=3D"43" align=3D"left"><FONT
> size=3D2>#M0704502X</FONT></TD>
> <td bgColor=3D#C0C0C0 height=3D"43" align=3D"left">
> <input type=3Dhidden name=3Ditem value=3D"b-3639^op1^Injection
> Pump, Light
> Duty Pickup^275.00^1^^^^13">
> <input type=3Dimage name=3Dnvadd src=3D"buy.gif" width=3D34
> height=3D21></td>
> <TD bgColor=3D#C0C0C0 height=3D"43" align=3D"left"><FONT
> size=3D2>$275.00</FONT></TD>
> </form>
>
> ignore all the table tags obviously since the entire table
> isnt here. and
> as far as the form code, that is required structure for our
> cart's cgi.
>
> thanks a TON people...