By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,442 Members | 1,341 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,442 IT Pros & Developers. It's quick & easy.

Form submit button not working with URL redirection

P: n/a
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

Aug 9 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
alu

"Hongyu" <fo*****@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
Aug 9 '05 #2

P: n/a
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

Aug 9 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.