Shaun
Shaun

Reputation: 5531

Get Value from Select in Javascript

Ok, I know this has been asked before (as I have viewed it myself) and it seems to work for everyone else except for me. Im terribly new to javascript, as I try not to use it cause its a nightmare to debug. But, here is my issue. I have a script that im working on for ajax to add an image to a gallery. The image is just an id that references another table in the database, and same for the gallery. Just two id's. Anyways, when I click on a link, I want it to run this script to add it to the database. But, its not working, and of course, javascript is horrible at letting you know where it fails. Here is the code for the script.

function addToGallery(img)
{

 var e = document.getElementById("galleries2");
 var gal = e.options(e.selectedIndex).value;
 window.alert("Gallery Id: "+gal+" Image Id: "+img);

       if (img=="")
       {
          document.getElementById("txtHint").innerHTML="";
          return;
       } 
       if (window.XMLHttpRequest)
       {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
       }
       else
       {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
       }
          xmlhttp.onreadystatechange=function()
       {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
       {
          document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
       }
    }
       xmlhttp.open("GET","addtogallery.php?img="+img+"gal="+gal,true);
       xmlhttp.send();
    }

Ok, so the above doesnt work. I added the alert message to try to troubleshoot as much as I could. If I put the alert box above the var's, and only ask it to reference the img variable, it works. Now, if I put it below and try to do what I did there, it just stops. No message box, no ajax, nothing. So, im betting it has something to do with the

var gal = e.options(e.selectedIndex).value;

I have also tried it with the [] instead of the () and nothing there either... Any help would be appreciated.

UPDATE Ok, so I have got past the problem with the select, now im trying to pass values with the xhtmlrequest.open method. I can pass 1 get value it seems, but not two. Here is the line of code in question

xmlhttp.open("GET","addtogallery.php?image="+img+"&gid="+gal,true);

Now, I know that img and gal are set because of an alert box that pops whenever this script is run to tell me that they are set. But when it gets to the php page its only putting out the img variable, and the gal variable is still not set. Anyone have this issue before?

Upvotes: 0

Views: 486

Answers (3)

Arman Bimatov
Arman Bimatov

Reputation: 1829

You definitely need square brackets, but also try changing your variable "e" to something else like "el". The function responds to a click as I understand, so it might be reserved for an event.

Try:

var galleries = document.getElementById("galleries2");
var gal = galleries.options[galleries.selectedIndex].value;

Upvotes: 1

I Hate Lazy
I Hate Lazy

Reputation: 48799

You need square brackets instead of parens.

 var gal = e.options[e.selectedIndex].value;

Your JavaScript environment should be giving you a nice TypeError saying something like "HTMLOptionsCollection is not a function"

Upvotes: 0

mbinette
mbinette

Reputation: 5094

I think what you want to do is:

var gal = e.options[e.selectedIndex].value;

Hope this helps.

Upvotes: 0

Related Questions