-1

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 the index.html

<html>
<head>
<meta charset="utf-8">
<title>jQuery Adapter &mdash; CKEditor Sample</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<script src="../ckeditor.js"></script>
<script src="../adapters/jquery.js"></script>
<script src="jquery.form.js"></script>

<script>
    CKEDITOR.disableAutoInline = true;

    $( document ).ready( function() {
        $( '#editor1' ).ckeditor(); // Use CKEDITOR.replace() if element is <textarea>.
        $( '#editable' ).ckeditor(); // Use CKEDITOR.inline().
    } );

    function setValue() {
        $( '#editor1' ).val( $( 'input#val' ).val() );
    }
</script>
</head>

<body>
<form id="f1" action="some.php" method="post" enctype="multipart/form-data">
<table>
    <tr>
        <td>Title:</td>
        <td><input type="text" name="articletitle" id="articletitle" required/></td>
    </tr>
    <tr>
        <td>Featured Image:</td>
        <td><input name="MAX_FILE_SIZE" value="2097152" type="hidden">
        <input name="image" id="image" type="file" required/></td>
    </tr>
    <tr>
        <td colspan="3"><textarea cols="80" id="editor1" name="editor1" rows="10"></textarea></td>
    </tr>
    <tr>
        <td><input id="save" type="submit" value="Save"></td>
    </tr>
</table>
</form>

<script type="text/javascript">

$(document).ready(function(){
    $("#save").click(function(){
        $("#f1").ajaxForm();
    });
});
</script>
</body>
</html>

Please help me guys on this one.

Jeemusu
  • 10,415
  • 3
  • 42
  • 64
  • 1
    This is because a submit buttons default behavior is to submit a form. You can prevent this by using `preventDefault()` in your click handler. [jQuery Docs](http://api.jquery.com/event.preventDefault/) – Jeemusu Sep 13 '13 at 06:39

5 Answers5

1

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.

firemeaway
  • 68
  • 6
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.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
0

TRY

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

});

OR

$("#save").click(function(){
        $("#f1").ajaxForm();
        return false;
});
rakeshjain
  • 1,791
  • 11
  • 11
0

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();

});
Jai
  • 74,255
  • 12
  • 74
  • 103
  • 1
    Don't say, `it doesn't work`. This is such a meaningless comment. Explain how it doesn't work. You asked how to stop your form redirecting to another page. This will almost certainly stop that from happening. If you want to ask a question about your AJAX failing, please start a new question. – Jeemusu Sep 13 '13 at 07:00
0

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;
});
Pevawi
  • 189
  • 2
  • 3