Welcome to WebmasterWorld Guest from 23.22.17.192

Forum Moderators: open

Message Too Old, No Replies

Jump Menu headache

How to add/pass variables to same page

   
1:34 pm on Jan 31, 2008 (gmt 0)

10+ Year Member



Hi there,

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.

5:03 pm on Jan 31, 2008 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Gads I hate Macromedia Javascript. :-) I'm not sure what you mean by "nothing happens," because it seems to be jumping fine here. Perhaps elaborate on what should happen and what is not. If it's not reconstructing the info area, it's something in your PHP?

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>
7:58 am on Feb 1, 2008 (gmt 0)

10+ Year Member



rocknbil, thanks for the response (and sorry for the delay in responding - I'd given up for the day).

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.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month