Form submit button not working with URL redirection

  • Follow


I am trying to implement a simple JavaScript of redirecting my window
to a new URL upon clicking a submit button. This is an easy task except
when I have to put an input type='submit' in front of the onClick
command. It always commits the CGI action, and skip the URL redirect
part. The example code is below

<form action=mycgi.pl>
  <input type='submit' name=submit value='Submit'
  onClick="window.location.href='http://myserver.com'">
</form>

I know if "type=button" is used here, the script can work, but I have
to use "type=submit" for some particular reasons in my project.

Does anyone have a good way to circumvent this problem? Appreciate your
time!

Hongyu

0
Reply forward (13) 8/9/2005 1:09:37 AM

"Hongyu" <forward@hongyu.org> wrote
> I am trying to implement a simple JavaScript of redirecting my window
> to a new URL upon clicking a submit button. This is an easy task except
> when I have to put an input type='submit' in front of the onClick
> command. It always commits the CGI action, and skip the URL redirect
> part. The example code is below
>
> <form action=mycgi.pl>
>   <input type='submit' name=submit value='Submit'
>   onClick="window.location.href='http://myserver.com'">
> </form>
>
> I know if "type=button" is used here, the script can work, but I have
> to use "type=submit" for some particular reasons in my project.
>
> Does anyone have a good way to circumvent this problem? Appreciate your
> time!
>
> Hongyu


From my limited knowledge of forms, I would think that your .pl file should
be handling the redirect.
Someone correct me if I'm mistaken...
I don't think the onclick is being skipped, it's just being immediately
replaced by the action.
You can test this as below, which results in an alert "button", then an
alert "form", then the google redirect.

<form action="http://www.google.com" onsubmit="alert('form');">
 <input type='submit' name=submit value='Submit' onClick="alert('button')">
</form>

-alu


0
Reply alu 8/9/2005 2:54:01 AM


Try this code...

<form action="mycgi.pl" onsubmit="return false;">
 <input type="submit" name="submit" value='Submit'
onClick="window.location.href='http://myserver.com'">
</form>

The ' onsubmit="return false;"' will prevent the form submission so
that the onclick javascript will be executed.

Hope it helps.

Binny V A

0
Reply binnyva 8/9/2005 8:32:08 AM

2 Replies
362 Views

(page loaded in 0.038 seconds)

Similiar Articles:













7/28/2012 2:53:14 AM


Reply: