Forum Moderators: open

Message Too Old, No Replies

Redirecting from a form submission

Thought it was simple...

         

zCat

7:55 am on Jun 9, 2006 (gmt 0)

10+ Year Member



For various reasons I want to put a simple form button to redirect users to another page using GET (and not POST). Unfortunately using GET means the submit button name and value end up in the URL. I thought JavaScript would offer a way round this by doing something like this:

<form method="get" action="/somepage.html" onsubmit="location.href='/somepage.html'">
<input type="submit" name="button" value="test" />
</form>

while retaining the original form behavior as a fallback for non-js enabled users. Unfortunately I can't get it to work - any attempts at redirection on submit have no effect. Is there any way to do this?

jshanman

12:43 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



You need to return a value in a function within the onsubmit event:

<form onSubmit="return checkSubmit()">

function checkSubmit() {
if (whatever) {
return false; //cancels submit
} else {
return true;
}
}

- JS

zCat

2:05 am on Jun 10, 2006 (gmt 0)

10+ Year Member



Ah, that did the trick :-). Many thanks!