Skip to content Skip to sidebar Skip to footer

My AjaxForm Redirects To A PHP Script

When I submit my form ajax always redirects to some.php, how can I prevent this from redirecting to the PHP script instead after submitting the form I want the page redirects to th

Solution 1:

The default behaviour of an <input type="submit"> is to submit the form.

There two ways to fix this. The first one is to simply make your input a button. That will prevent the browser to send the form.

A better way though is to be JavaScript-independent and use what @Jeemusu said, use preventDefault() to prevent the submit to occur.


Solution 2:

Try this:

$(document).ready(function(){
    $("#save").click(function(e){
        e.preventDefault();
        $("#f1").ajaxForm();
    });
});

It should prevent your input from doing its default behavior of redirecting the page.

Edit:

To redirect to another page you should use something like:

window.location.href = 'http://www.google.com';

on the ajax's Success event.

Edit 2:

Try this fiddle: http://jsfiddle.net/4hF6e/3/ Make sure your references are correct. Seems to me the AjaxForm plugin has some way to prevent default behavior on the button.


Solution 3:

TRY

$("#save").click(function(e){
        e.preventDefault();
        $("#f1").ajaxForm();

});

OR

$("#save").click(function(){
        $("#f1").ajaxForm();
        return false;
});

Solution 4:

instead of using type="submit" you can use:

type="button"

or you can make use of .preventDefault();:

$("#save").click(function(e){
    e.preventDefault();
    $("#f1").ajaxForm();

});

Solution 5:

Or you could attach to the event "onsubmit" of the form. And return false in that function. i.e.:

$('#f1').submit(function() {
  // do your ajax stuff... and when completed (success) redirect 
  // using document.location.href = 'index.html';
  return false;
});

Post a Comment for "My AjaxForm Redirects To A PHP Script"