简体   繁体   中英

Redirect after form POST

I am working on saving information submitted through a form to parse.com and then redirecting to an alternate page once complete. I have the POST working correctly but cannot find the easiest way to redirect to the new page. Here is my code:

 <script>
        function validateForm() {
        Parse.initialize("1oFe8znPN2CwcEj14eIYaIOuxGtW35ZpmOazlG3H", "fJhaxIenhcwAdp66T14LKw4OukJ3lG6DLpkq1JVV");
        var TestObject = Parse.Object.extend("iOS");
var testObject = new TestObject();
  testObject.save({Phone: document.forms["appForm"]["phone"].value },{
  success: function(object) {
    $(".success").show();
  },
  error: function(model, error) {
    $(".error").show();
  }
});}

and

            <input type="text"  name="phone" id="contact_email"  class=" form-control input-email" placeholder="Phone Number">

            <input type="submit" id="sendingbtn" class="btn" value="Submit">

I had onclick="location.href='appsuccess.html';" in the submit input but it would push sometimes before the POST. Any suggestions? Thanks

What about using

window.location.href = 'http://redirecturl/';

in the success function? ( How to redirect to another webpage in JavaScript/jQuery? )

Since you display a success message on your page, so I would suggest redirecting the user after a delay using setTimeout. The timeout should be initiated after the post returns successfully. So, you should modify your success function to be:

function(object) {  
  $(".success").show();  
  window.setTimeout(function() {  
    location.href = "appsuccess.html";  
  }, 1500);  
}  

This will cause the site to redirect after a 1.5 second delay. You can adjust the timing as needed for the effect you want. Documentation on setTimeout: https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout

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