2

I need help. When I submit this form I want it to automatically redirect to the value option (which is url) in the select list.

So on pressing submit for Ahmedabad selected in select list it should redirect to http://intranet.guj.nic.in/passport

Currently it is not redirecting. What is the best way to do it using inline javascript code?

<html><body>
<form  id="pass-form" >
<select id="pass-dropdown" ><option selected="selected">Select Passport Office
for Status page </option><option value="http://intranet.guj.nic.in/passport/">Ahemadabad
</option><option value="http://passportstatus.nic.in/passmain.php?city=asr">Amritsar
</option><option value="http://rpobangalore.gov.in/npassport.asp">Bangalore
</option><option value="http://passportstatus.nic.in/passmain.php?city=bly">Bareilly
</option></select>
<input type="submit"  onsubmit="var sel = document.getElementById('pass-dropdown'); window.location = sel.options[sel.selectedIndex].value" />
</form>
</body>
</html>
Mate
  • 4,976
  • 2
  • 32
  • 38
AgA
  • 2,078
  • 7
  • 33
  • 62
  • You might want to check out: http://stackoverflow.com/questions/138884/when-should-i-use-inline-vs-external-javascript and http://css.dzone.com/news/why-inline-css-and-javascript- – Sam Jan 05 '13 at 10:49
  • Also why don't you just use links instead of the drop down list? Alternatively you could do this via a server side script. Just set headers to equal $_GET['form']. – Sam Jan 05 '13 at 10:51
  • Because the form has been submitted, the `window.location` will not fire. You need to prevent the default submission behaviour, or handle it on the server side. Something like `` Should do the job. – PassKit Jan 05 '13 at 10:53
  • @Sam I don't want Google to know where I'm redirecting. – AgA Jan 05 '13 at 10:59

4 Answers4

8

onsubmit is an event of the <form> element and not of the submit button.

Change your form code to:

<form id="pass-form" onsubmit="window.location.href = 'newlocation'; return false;">
...
    <input type="submit" value="Submit" />
</form>
techfoobar
  • 65,616
  • 14
  • 114
  • 135
3

In some cases you might have to use return to ensure the desired return value gets used:

<form onsubmit="return redirectMe();">
  <input placeholder="Search" type="text">
</form>

... more code here ...

function redirectMe() {
  window.location.replace("http://stackoverflow.com");
  return false;
}

Source: How to pass an event object to a function in Javascript?

Community
  • 1
  • 1
apebeast
  • 368
  • 3
  • 15
1

Change the type = "submit" to button

<input type="button" onclick="var sel = document.getElementById('pass-dropdown'); window.location = sel.options[sel.selectedIndex].value" />

Update

<input type="button" onclick="var sel = document.getElementById('pass-dropdown'); 
       var frm = document.getElementById("pass-form"); frm.action = sel; frm.submit();" />

It would be better if you put that in a function and call the function instead

codingbiz
  • 26,179
  • 8
  • 59
  • 96
  • Thanks but I want to keep type="submit" is it possible to change the ACTION in form and submit it then in the same way? – AgA Jan 05 '13 at 10:58
  • Yes. `frm = document.getElementById("pass-form"); frm.action = sel; frm.submit();` – codingbiz Jan 05 '13 at 11:03
0
<form  id="pass-form" onsubmit="var sel = document.getElementById('pass-dropdown'); window.location = sel.options[sel.selectedIndex].value" >
    ...
    <input type="submit" />
</form>

You must do the submit attribute to the form tag. Then it should work. http://www.w3schools.com/jsref/event_form_onsubmit.asp

ijo
  • 55
  • 8
  • 1
    Use `window.location.replace(sel.options[sel.selectedIndex].value)` instead of `window.loaction = ...` http://www.w3schools.com/jsref/met_loc_replace.asp – ijo Jan 05 '13 at 11:07