Jump to content

drop-down list automatic url redirect


murfitUK

Recommended Posts

I really can't get my head round javascript so I've copied code from elsewhere. I found this somewhere else in this forum.This is inside the <head> tags:

<script type="text/javascript">function go(){window.location=document.getElementById("menu").value;}</script>

And this is part of the html:

<form><select id="menu" onchange="go()"><option>--Select a page--</option><option value="http://www.w3schools.com">W3Schools</option><option value="http://www.microsoft.com">Microsoft</option><option value="http://www.altavista.com">AltaVista</option></select></form>

On the whole, it works fine. I tried it with javascript disabled (to cover all eventualities) but, of course, it doesn't work. What would I need to do to get it working for users with no javascript (or javascript disabled)? I thought about adding this

</select><noscript><input type="submit" value="go" /></noscript></form>
but it doesn't work.Thanks.
Link to comment
Share on other sites

You can add a submit button, and have PHP process the result like this:<?phpheader("Location: " . $_POST['menu']);?>Make sure to give a name attribute to your <select> element.

Link to comment
Share on other sites

Ingolme's method is gauranteed to work even if JavaScript is disabled. I would suggest implementing his suggestion and then adding a JavaScript function to intercept normal functionality to be a bit more fancy.HTML

<form action="DropDownRedirect.php" method="post"><select id="menu" onchange="go(this)"><option value="0">--Select a page--</option><option value="http://www.w3schools.com">W3Schools</option><option value="http://www.microsoft.com">Microsoft</option><option value="http://www.altavista.com">AltaVista</option></select> <input type="submit" name="go" id="go" value="Go"/></form><script type="text/javascript">  //if Js is enabled hide the go button  document.getElementById('go').style.display = 'none';  //redirect  function go(dropdown) {	var v = dropdown.options[dropdown.selectedIndex].value;	if(v != "0") {	  window.location = v;	}  }</script>

PHP DropDownRedirect.php

<?phpif(isset($_POST['go']) && $_POST['menu'] != "0") {  header("Location: " . $_POST['menu']);}?>

Link to comment
Share on other sites

Problem has already been solved, but just for future reference, when javascript is off, its kinda obvious that Javascript code isn't going to work. Easiest solution I can give you is 1 of 2. The first being "If they turn javascript off, then screw them." The second being implement something that works without javascript, such as ingolme has already posted, and just use Javascript to make it quicker/cooler looking. The second option should always be what you attempt to do.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...