0

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?

7
  • Could you please indent your code appropriately? Commented Sep 22, 2012 at 16:54
  • You should really check out how to to debug javascript and make use of the error console Commented Sep 22, 2012 at 16:57
  • It (must) should work with square brackets []. Are you sure galleries2 is the id of the select element? Post some html and delete the second part of the javascript which is irrelevant. Commented Sep 22, 2012 at 16:57
  • JavaScript debugging is really easy if you use more than alert() traces. Look into Chrome's Developer Tools or Firebug for Firefox. They both have full debugging, logging with console.log(), and much more. Commented Sep 22, 2012 at 16:58
  • var gal = e.options(e.selectedIndex).value; What is the purpose of this line? Commented Sep 22, 2012 at 16:58

3 Answers 3

1

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;
Sign up to request clarification or add additional context in comments.

Comments

0

I think what you want to do is:

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

Hope this helps.

Comments

0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.