-2

I am looking for a basic jQuery function to submit a form without refreshing the page. I had this once but I can't find it, all I can find is the long boring and complicated ones you have to update to add an input field into the form.

From what i remember the code includes the URL of the process file, the form id and the #results id.

7
  • please read FAQ, search before asking, this Q is going to be closed soon or marked as duplicate. FYI: you are looking for AJAX solution (no refresh).
    – Kyslik
    Commented Jun 8, 2013 at 11:13
  • Would i post if i could find anything? learn to read. Commented Jun 8, 2013 at 11:17
  • jQuery AJAX submit form look for first answer that is what you need
    – Kyslik
    Commented Jun 8, 2013 at 11:26
  • @Kyslik "without refreshing the page"... Commented Jun 8, 2013 at 11:29
  • 1
    @user1926053 of course it is.
    – moonwave99
    Commented Jun 8, 2013 at 11:33

2 Answers 2

1

you can use Ajax form submit as follow;

<form id="formId" action="url" method="post">

</form>

 <script type="text/javascript">
    var form = $('#formId');
    form .submit(function () {
    $.ajax({
        type: "POST",
        url: "url",// url which you want to post
        data: formData,
        success: function (data) {
            alert('success');
        },
        error: function(jqXHR, textStatus, errorMessage) {
            alert(errorMessage); // Optional
        }
    });
});
</script>
1
    <?php
        $message = '';
        if( isset( $_GET['name'] ) ) {
            if( empty( $_GET['name'] ) ) $message = 'Name is required';
            else $message = 'Form submitted successfully.';
        }
    ?>
    <form id="ajax-form">
        <input name="name" type="text">
        <input type="submit">
        <div id="message"> <?php echo $message; ?> </div>
    </form>

    <script>
        $( '#ajax-form' ).submit( function( e ) {
            e.preventDefault();
            var query = $(this).serialize();
            $(this).find('#message').load( window.location.href + '?' + query + ' #ajax-form #message' );
        });
    </script>

Not the answer you're looking for? Browse other questions tagged or ask your own question.