admin管理员组

文章数量:1410682

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

本文标签: javascriptHTML anchor tag onclick to Submit form and Redirect to href linkStack Overflow