Form submit button not working with URL redirection

H

Hongyu

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
 
A

alu

Hongyu said:
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
 
B

binnyva

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top