javascript - HTML anchor tag onclick to Submit form and Redirect to href link - Stack Overflow

I'm trying to submit the form and redirect to href url specified in anchor tag href attribute.Form

I'm trying to submit the form and redirect to href url specified in anchor tag href attribute.

Form:

<form id="myform" method="POST">
    <a href='' onclick='submitResponse()'>SUBMIT</a>
</form>

JavaScript:

function submitResponse() {
    document.forms[0].submit(); 
}

But it only redirects to the link and form is not getting submitted.

I also tried this and is not working either.

<form id="myform" method="POST">
    <a href='#' onclick='submitResponse()'>SUBMIT</a>
</form>

function submitResponse() {
    window.location.href = "" ;
    document.forms[0].submit(); 
}

I'm trying to submit the form and redirect to href url specified in anchor tag href attribute.

Form:

<form id="myform" method="POST">
    <a href='http://www.google.' onclick='submitResponse()'>SUBMIT</a>
</form>

JavaScript:

function submitResponse() {
    document.forms[0].submit(); 
}

But it only redirects to the link and form is not getting submitted.

I also tried this and is not working either.

<form id="myform" method="POST">
    <a href='#' onclick='submitResponse()'>SUBMIT</a>
</form>

function submitResponse() {
    window.location.href = "http://www.google." ;
    document.forms[0].submit(); 
}
Share Improve this question asked Sep 3, 2015 at 11:09 RamRam 1,8033 gold badges22 silver badges41 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

You must to read about html forms. To put an action for the form use action attribute

 <form action="http://google." method="POST">

So your code looks like this

<form id="myform" method="POST">
    <a href='#' onclick='submitResponse()'>SUBMIT</a>
</form>

function submitResponse() {
    document.forms[0].action = "http://www.google." ;
    document.forms[0].submit(); 
}

Read up on forms here:

forms

The soloution to your problem:

<form action="http://google." method="POST">

When you execute the form submitt and if you dont specify an url adress in the action the page will simply reload.

the only reason you get redirected is because of this:

 window.location.href = "http://www.google." ;

EDIT:

consider this:

function submitResponse() {
    $('form').submit(function(){
        $.post('http://google.', function() {
          window.location = 'http://google.';
        });

      });
}

The form gets submitted and in the success function you redirect to the desiered site.

the reason your code does not work is since the redirect does not wait for the form to submitt, it directs right away.

Simply update the action attribute of the form tag:

var submitResponse = function(url) {
  var form = document.getElementById('myform');
  form.action = url;
  form.submit();
};
<form id="myform" method="POST">
  <a href='http://www.google.' onclick='submitResponse(this.href);return false;'>SUBMIT</a>
</form>

If you are submitting to a URL you can simply return the HTTP response as below.

HTTP/1.1 302 Found
Location: http://www.google.

References:

http://www.w3schools./tags/ref_httpmessages.asp 

https://en.wikipedia/wiki/HTTP_302

Update

If you are using

ASP.Net MVC

http://www.dotnet-tricks./Tutorial/mvc/4XDc110313-return-View()-vs-return-RedirectToAction()-vs-return-Redirect()-vs-return-RedirectToRoute().html

http://stackoverflow./questions/7892094/mvc-redirect-to-index-from-another-controller

PHP

http://stackoverflow./questions/768431/how-to-make-a-redirect-in-php

JAVA

http://stackoverflow./questions/2659000/java-how-to-find-the-redirected-url-of-a-url

http://stackoverflow./questions/15057329/how-to-get-redirected-url-and-content-using-httpurlconnection

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744941893a4602375.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信