Forum Moderators: open
If anyone could shed light on this, I'd appreciate it (I've been tasked with finishing an old abandoned website whose original coder has long departed for more lucrative gigs)
I have a jump menu on a page that is dynamically created. Underneath that menu is some product details (price, weight etc) that relate to whatever item has been selected in the drop down. If the user clicks another item, an 'onchange' event reloads the page with the new details. My problem is, I think, that I don't know how to reference the detail area with the Javascript function. What I have at the mo' is this:
<SCRIPT LANGUAGE="JavaScript">
<!--
function MM_jumpMenu(selObj,restore) {
eval("window.location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</SCRIPT>
and the form and text area is:
<form name="form2" method="post" action="page.php">
<SELECT NAME="bob" onChange="MM_jumpMenu(this,1)">
<OPTION VALUE="page.php?var1=1&var2=1" SELECTED>Item 1</OPTION>
<OPTION VALUE="page.php?var1=1&var2=2" SELECTED>Item 2</OPTION>
...
</SELECT>
</form><TABLE>
<TR>
<TD>Price:</TD>
<TD><?php echo ...price...?></TD>
</TR>
<TR>
<TD>Weight:</TD>
<TD><?php echo ...price...?></TD>
</TR>
</TABLE>
'var2' is the new variable added to the URL when it refreshes. At the moment nothing happens so if this makes sense to anyone, I'd appreciate your feedback.
In the meantime: you have SELECTED on all items. This causes the last item in your list to be selected.
Selected should be only set for the single selected item in the list, and then only if it's not the first item.
Second, it's a good idea to have the first item a blank.
Last, the eval function is for evaluating math, not text - it works fine, and is often (ab)used to evaluate strings, it's just not what it's for.
A revised 'script:
<script type="text/javascript">
function jump (selObj) {
var sel = selObj.options[selObj.selectedIndex].value;
if (sel!= '') { window.location = sel; }
return false;
}
</script>
<form name="form2" method="post" action="page.php">
<select name="bob" onChange="jump(this)">
<option value="">Select</option>
<option value="page.php?var1=1&var2=1">Item 1</option>
<option value="page.php?var1=1&var2=2" >Item 2</option>
</select>
</form>
Lol, the 'selected' tag was on all the options because I was lazy when preparing this post and just cut 'n' pasted the first line.
You were right about it being a PHP problem. I stripped the page right down to the basics and started again and yes, it did jump ok. I had thought it was only updating a small area of the page and I wasn't sure how to reference that area, but it turns out that it was effectively building a whole new page, so that was much easier to deal with.
I'll certainly give that code of yours a whirl, so thanks again for your time and suggestions. Much appreciated.