We help IT Professionals succeed at work.

How to use this SelectBox Javascript??

Nottingham
Nottingham asked
on
Medium Priority
370 Views
Last Modified: 2008-03-06
I have this script that passes values from one selectbox to another.

I need to add the values that are selected to a database.

The script works brilliantly in the browser.

But when I post the values of the form to a php script the values aren't there - I know this is something simple - but what??

Please see the script at http://www.european-machining.com/jscript.htm
Comment
Watch Question

Nitin SontakkeDeveloper
CERTIFIED EXPERT

Commented:
I have following to say:

01. In my opinion, the values which are selected are only sent in HTTP posting. So, you will need to manually selected all items in "To" list-box. This can be done with the help of a function as follows:

<Script Language="javaScript">
<!--
function selectAll()
{
   var intCount = document.form.list2.length;
   var intIndex = 0;
   for(intIndex = 0 to intCount-1)
   {
      document.form.list2.options[intIndex].selected = true;
   }
   return true;
}
// -->
</Script>

02. Modify your html as follows as the above functions is called:

<input type="submit" onClick="javaScript:selectAll()">

03. Please make values shorter as shown below, as they are sent as part of posting. You can always expand them again on server.

<OPTION VALUE="A">Adhesive</OPTION>
<OPTION VALUE="B">Boxes</OPTION>
<OPTION VALUE="C">Cans</OPTION>
<OPTION VALUE="S">Strapping</OPTION>
<OPTION VALUE="M">Machines</OPTION>
<OPTION VALUE="X">X-ray Machines</OPTION>

04. It is a good practise to name your html form even if it is not compulsory.

Unfortunately, i was not been able to test this on my local machine.

Nitin SontakkeDeveloper
CERTIFIED EXPERT

Commented:
Extremely sorry for mixing vb with javaScript. Please modify the for loop to look as follows:

 for(intIndex = 0; intIndex++; intIndex < intCount)
CERTIFIED EXPERT
Top Expert 2005

Commented:
JavaScript loop should be

for(intIndex=0; intIndex<intCount; intIndex++)


If Nitin's suggestion doesn't work, then you might want to consider a messier way to to this. As each item is added or removed, you add it to  a string stored in a hidden field. When you post the form, the contents of the hidden field will be posted. You can then use the split function to separate the elements so that you can manipulate them as necessary.

Nitin SontakkeDeveloper
CERTIFIED EXPERT

Commented:
Thanks "fritz_the_blank", for refreshing my memory. You don't work in particular language in for couple of months and it starts getting rusty.

One problem, i am not getting mails from this site, have an idea, what to do?
inside your PHP, you'll also need to do something with the value of the "To" field. It will most likely look like this: "Cans, Machines".

Best to try and split the string into an array, and then do a loop adding each element of the array into the database. I selected the above two objects in your example, and the results was: List2=Cans.

In VB script, I'd do this:

strList2 = Request.Form("List2)
arrObjects = Split(strList2, ", ")
If Not IsArray(arrObjects) Then '// for when you only have one.
  ReDim arrObjects(0)
  arrObjects(0) = strList2
End If

For i = lBound(arrObjects) To uBound(arrObjects)
  Response.Write "List2 = " & arrObjects(i) & "<br>"
Next
CERTIFIED EXPERT
Top Expert 2005

Commented:
No mails here either. I will see if anyone has posted a message to community support.

Fritz the Blank
CERTIFIED EXPERT
Top Expert 2005

Commented:
Nottingham,

Have any of these comments helped you? If so, please accept one of them as an answer.

Thank you,

Fritz the Blank

Author

Commented:
Thanks for the added php info

Explore More ContentExplore courses, solutions, and other research materials related to this topic.