Click to See Complete Forum and Search --> : Friday Forms Hell


fuxmyl
02-14-2003, 12:37 PM
I have a page and I'm using this script to populate a textbox from a drop down menu:

<script language="JavaScript"><!--
function updateForm(what,count) {
selected = what.partnumber1.selectedIndex;

value = what.partnumber1.options[selected].value;

for (var i = 0, oldpos = -1; i<count-1; i++) {
pos = value.indexOf('&',oldpos+1);
data = value.substring(oldpos+1,pos);
what.elements['description' + i].value = value.substring(oldpos+1,pos);
oldpos = pos;
}
what.elements['description' + i].value = value.substring(oldpos+1,value.length)

}

//--></script>

Here is the URL of the Page: http://in.northern-tech.com/quote/test_quote.asp

This works for the first drop down menu and text box, but when I try to get the next dropdown menu in line to populate the next text box then the first drop down menu stops working. The is the code I used to get the second box working:

<script language="JavaScript"><!--
function updateForm(what,count) {
selected = what.partnumber1.selectedIndex;

value = what.partnumber1.options[selected].value;

for (var i = 0, oldpos = -1; i<count-1; i++) {
pos = value.indexOf('&',oldpos+1);
data = value.substring(oldpos+1,pos);
what.elements['description' + i].value = value.substring(oldpos+1,pos);
oldpos = pos;
}
what.elements['description' + i].value = value.substring(oldpos+1,value.length)

}

function updateForm(what,count) {
selected = what.partnumber2.selectedIndex;

value = what.partnumber2.options[selected].value;

for (var i = 0, oldpos = -1; i<count-1; i++) {
pos = value.indexOf('&',oldpos+1);
data = value.substring(oldpos+1,pos);
what.elements['adescription' + i].value = value.substring(oldpos+1,pos);
oldpos = pos;
}
what.elements['adescription' + i].value = value.substring(oldpos+1,value.length)

}


//--></script>

Here is the URL that used this code: http://in.northern-tech.com/quote/test_quote1.asp

as you can see, the second drop down list now works but the first drowdown list stopped working. What am I doing wrong?

khalidali63
02-14-2003, 02:29 PM
I am not sure if I got it completely right,but I have done it so tht it works with all the drop down,
Since its way too confusing to clearly understand the goals,I could not do more.

let me know.

cheers

Khalid
btw
file is attached as text file, save it as html and run it.

Nedals
02-14-2003, 02:35 PM
I'm not sure exactly what you trying to accomplish (don't understand the need for the loop!!), but I'll assume you are attempting to put a description in the text box to the right of the select element.

Your .. onChange="updateForm(this.form,1) does not tell the function which text box is to be updated

Try something like this...
function updateForm(what,textbox_name) {
selected = what.partnumber1.selectedIndex;
selected_value = what.partnumber1.options[selected].value;
what.elements[textbox_name].value = selected_value
}

onChange="updateForm(this.form,'textbox_name')

fuxmyl
02-14-2003, 04:10 PM
that code works great, but when I try to make more that one dropdown menu work, it screws it up. I need drop-down menu named "partnumber1" to populate text box named "description0" and I need Drop-down menu named "partnumber2" to populate text box named "adescription0", and so on. right now i have only put in descriptions on the part number 2000009 in the first 2 text boxes. how can I make all the drop down lists work at the same time without effecting each other? Do I put the javascript in the head or right before the dropdown list? Here is a link to the page I'm working on with the code you sent me:

http://in.northern-tech.com/quote/test_quote1.asp

thanks for your help

Nedals
02-14-2003, 05:46 PM
Pondering......
OK. I prefer to put all my JS stuff in the HEAD section.
Anyway, put this in the head section (you only need it once)
<SCRIPT language=JavaScript>
<!--
function updateForm(what,descr) {
selected = what.selectedIndex;
selected_value = what.options[selected].value;
document.FrontPageForm1[descr].value = selected_value // '_' has been removed
}
//-->
</SCRIPT>

Get rid of the '.form' in these lines
onChange="updateForm(this,'textbox_name')

Get rid of the '_' in your FORM name (it's sometimes problematic)

I think that should do it...

Nedals
02-14-2003, 08:40 PM
Here's another bit of script you may wish to incorporate

function Validator(theForm) {
var fieldList = new Array('price1','price2'); // names of price fields to be tested
var re = /^\d+\.?\d*$/;
// this is a 'regular expression' (regex) that tests for a number (with or without a decimal point)
// ^ beginning of data
// \d+ decimal char - 0..9; (+) one or more times
// \.? decimal point; (?) zero or one time
// \d* decimal char - 0..9; (*) zero or more times
// $ end of data
for (var i=0; i<fieldList.length; i++) {
if (!re.test(theForm[fieldList[i]].value)) { alert('not OK'); return false; }
}
return true;
}

This will replace all that validation stuff you have and give you an opportunity to learn all about regular expressions.

fuxmyl
02-18-2003, 03:39 PM
Ok,

I used the latest script that you gave me on this page:

http://in.northern-tech.com/quote/test_quote1.asp

The form's name is 'quote' and I have taken the '.form' out of the onchange equations. Here is the script for the head:

<SCRIPT language=JavaScript>
<!--
function updateForm(what,descr) {
selected = what.selectedIndex;
selected_value = what.options[selected].value;
document.quote[descr].value = selected_value
}
//-->
</SCRIPT>

as you will see when you try it, there is an error happening that says 'document.quote' is null or not an object. What does that mean? Hey Nedals, you know any good books for beginner Java? I looking for the very basics. Thanks for your help.

Nedals
02-18-2003, 04:06 PM
This line
document.quote[descr].value = selected_value
should read..
document.FrontPage_Form1[descr].value = selected_value

(ie: document.form_name.element_name.value)

Hey Nedals, you know any good books for beginner Java?
Do you mean 'Java' or 'Javascript'. They are VERY different!
If Java, I cannot recomend anything..
If Javascript, I started, way back, with "Javascript for the world wide web" by Ted Gesing and Jeremy Schneider (about $20.00). It gives lots of examples to work through BUT it does not really explain the why's nor show the full syntax and properties of a method. You should also download the Javascript documentation from Netscape. That should get you started.