Javascript menu only works once?

Discussion in 'JavaScript' started by dlow123, Oct 1, 2007.

  1. #1
    Hello All,

    I have searched far and wide and I cannot find the exact answer to the question I have. The issue is this, I need to ultimately pass an 'id' and from an 'item' in my db (mysql) to the browser when a user makes a selection from a menu I have.

    Currently I am able to populate the menu with my db items just fine, however, the best I can get this script to do is recognize the first selection! Any selection after that doesn't work at all. As I am testing right now, I simply print out the id and name of the selection from the menu on the same screen, right below the drop down menu.


    I am currently using the following code with no luck:

    
    
    $sql="SELECT id, name FROM items WHERE something = 'No' ORDER BY name asc";
    $result=mysql_query($sql);
    
    $options="";
    
    while ($row=mysql_fetch_array($result)) {
    
        $id=$row["id"];
        $name=$row["name"];
        $options.="<OPTION VALUE=\"$id/testpage.html\">".$name;
    }
    
    echo " 
    
    // There is more code before this form but I have left it out for readability //
    
    <form action=\"GoToLocation.cgi\">
    		<select id=\"newLocation\">
    			<option selected=\"selected\">Select an Item</option>
    			$options
    		</select>
    		<noscript>
    			<input type=\"submit\" value=\"Go There!\" />
    		</noscript>
    	</form>
    ";
    
    PHP:
    The following is the javascript code from file (script01.js):

    
    
    window.onload = initForm;
    window.onunload = function() {};
    
    function initForm() {
    	document.getElementById("newLocation").selectedIndex = 0;
    	document.getElementById("newLocation").onchange = jumpPage;
    }
    
    function jumpPage() {
    	var newLoc = document.getElementById("newLocation");
    	var newPage = newLoc.options[newLoc.selectedIndex].value;
    
    	if (newPage != "") {
    		window.location = newPage;
    	}
    }
    
    
    Code (markup):
    I am definitely doing something wrong but I can't figure it out for the life of me.
     
    dlow123, Oct 1, 2007 IP
  2. dlow123

    dlow123 Active Member

    Messages:
    270
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    73
    #2
    Got it working!!

    Not sure if anyone is interested but I had to change:

    from this

     $options.="<OPTION VALUE=\"$id/testpage.html\">".$name; 
    Code (markup):
    to this

     $options.="<OPTION VALUE=\"http://root_of_website/$id/testpage.html\">".$name; 
    Code (markup):
     
    dlow123, Oct 1, 2007 IP