简体   繁体   中英

Redirect page after form submitting a form

<body onload="document.action.submit()">
<form name="action" method="post" action="https://en.educaplay.com/en/registrar.php" >
<input type="hidden" name="action" value='loginTicket'>

    <input name="ticket" type="text" id="ticket" value="<%= session[:educa_key] %>">
    <input type="submit" name="entrar" id="entrar2" value="Sign in" class="btn">
</form>

Now, I want to redirect the submitter to any page of my choice after the form data has been submitted, but the action resides on another website where I cannot edit. Is it possible to redirect the user to any page after he has submitted the data to that site? I have checked a number of suggestions but not sucess at this point. thanks in advance

When you submit a form, there will be an automatic browser redirection to the URL you specified in the action attribute of your form. Here is a minimal example:

 <form action="http://stackoverflow.com"> <input type="text"> <input type="submit" value="Submit"> </form> 

Now, if you want to code a form properly, you should follow the Post/Redirect/Get pattern. Ajax is a viable solution for what you want to do:

(function () {
  // Dummy submit handler
  function handler() {
    var xhr = new XMLHttpRequest(),
        url = "action.php",
        params = "foo=Foo&bar=Bar";

    xhr.open("POST", url, true);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    xhr.onreadystatechange = function() {
      if(xhr.readyState == 4 && xhr.status == 200) {
        console.log(xhr.responseText);
        location.href = "http://stackoverflow.com";
      }
    }

    xhr.send(params);
  }
})();

In your registar.php file, add header('Location: https://www.example.com/example.html');

This isn't necessarily JavaScript, but it's probably the most efficient method.

If you do not have to wait for the answer, you can use javascript (jquery if you want and can) for this:

$("#entrar2").click(function(){
    $(location).href(link);
});

Your submission will cancel the redirect.

<form name="form1" id="form1" method="post" onsubmit="return redirect()">  
   <input type="submit" class="button4" name="order" id="order" value="Place Order" >
</form>

<script>
    function redirect() {
        window.location.replace("login.php");
        return false;
    }
</script>

OR

document.getElementById("formId").onsubmit=function() {
   window.location.replace("https://jsfiddle.net/");
   return false;
}    

You have to send your data to other website using curl before form submitted.Where you can process your data or store it . And after successful response from other page you can redirect user to any page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM