Form submission using ajax and page view moderation after the submission

Sagiruddin Mondal

At this moment I am using laravel. In this context I am having a form which is successfully submitted by using ajax to a controller. and that controller make it to the database. But the problem is as the ajax is doing its job the whole page remain unmoved / unchanged after the submission even the database is updated.

Now what I want

I want to give feedback to the user that your post is successfully submitted there. or what I want to do in further, I want to refresh the section in which the post is collected from the database as this post can be retrieved from there. But by using ajax only.

So there is no need to collect the whole page or refresh.

here is my form structure

`

    {{ Form::open(array('route' => array('questions.store'), 'class' => 'form-horizontal'  )) }}

    blah blah blaaa .......


    <script type="text/javascript">

                        $(".form-horizontal").submit(function(e){
                        $(this).unbind("submit")
                        $("#ask").attr("disabled", "disabled")


                        var that = $(this),
                            url = that.attr('action'),
                            type = that.attr('method'),
                            data = {};
                        that.find('[name]').each(function(index, value){
                                var that = $(this),
                                name = that.attr('name'),
                                value = that.val();

                                data[name] = value;

                        });

                        $.ajax({
                            url: url,
                            type: type,
                            data: data,
                            success: function(response){
                                console.log(response);
                            }
                        });

                        return false;
                    });
                    </script>


{{ Form::close() }}

` As it is very much visible that the post is updated through a route & controller I want to have another dive and a success message at this script to be displayed after the success of posting. I am looking for some professional structure using what there is minimal need to have interaction with the server side and give user a better page viewing experience.

Thanks a lot for helping me in this research.

Gadoma

I am not sure if I understand you well, but if you want to notify the user about the result of an ajax-called db update you need to have

  1. a route for the ajax save db call - it should point to a method that does the db update.
  2. the db update method should return some value indicating the success/failure of update (for example OK or FAIL)
  3. the only result of calling the method will be just plain text page with OK or FAIL as body
  4. fetch the result by ajax and inform user accordingly (after form submit button)

check out the below code for ajax call itself (inside the form submit handler) to see what I mean

    var db_ajax_handler = "URL_TO_YOUR_SITE_AND_ROUTE";

    var $id = 1; //some id of post to update
    var $content = "blablabla"  //the cotent to update

        $.ajax({
                cache: false,
                timeout: 10000,
                type: 'POST',
                tryCount : 0,
                retryLimit : 3,
                url: db_ajax_handler,
                data: { content: $content, id: $id }, /* best to give a CSRF security token here as well */ 
                beforeSend:function(){

                },
                success:function(data, textStatus, xhr){

                     if(data == "OK") 
                     {
                        $('div.result').html('The new Question has been created');
                     }
                     else
                     {
                       $('div.result').html('Sorry, the new Question has not been created');  
                     }


                },
                error : function(xhr, textStatus, errorThrown ) {
                    if (textStatus == 'timeout') {
                        this.tryCount++;
                        if (this.tryCount <= this.retryLimit) {
                            //try again
                            $.ajax(this);
                            return;
                        }
                        return;
                    }
                    if (xhr.status == 500) {
                        alert("Error 500: "+xhr.status+": "+xhr.statusText);

                    } else {
                        alert("Error: "+xhr.status+": "+xhr.statusText);

                    }
                },
                complete : function(xhr, textStatus) {

                }
            });

EDIT: as per comment, in step 2 (the method that is called with AJAX) replace

if($s) 
{ 
    return Redirect::route('questions.index') ->with('flash', 'The new Question has been created'); 
}

with

return ($s) ? Response::make("OK") : Response::make("FAIL");

EDIT 2: To pass validation errors to the ajax-returned-results, you cannot use

return Response::make("FAIL")
        ->withInput()
    ->withErrors($s->errors());

as in your GIST. Instead you have to modify the suggested solution to work on JSON response instead of a plain text OK/FAIL. That way you can include the errors in the response and still benefit from the AJAX call (not having to refresh the page to retrieve the $errors from session). Check this post on the Laravel Forum for a working solution - you will get the idea and be able to fix your code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Don't reload page after form submission using ajax and jquery

From Dev

Not able to refresh the partial view after a form submission from another partial view within it, using ajax in mvc razor

From Dev

Redirect after AJAX form submission

From Dev

Redirect after AJAX form submission

From Dev

Form submission using AngularJS/AJAX

From Dev

Using jQuery AJAX to call a php page on form submission

From Dev

jQuery - stop form submission from changing page using ajax

From Dev

Form Submission not working second time after submiting using Jquery and Ajax

From Dev

Ajax form submission on a form created using jquery

From Dev

php return to page after form submission

From Dev

Display the same page even after form submission

From Dev

redirecting to another page after form submission

From Dev

php return to page after form submission

From Dev

Modal form Submission to PHP using Jquery .ajax

From Dev

Ajax form submission issues

From Dev

Ajax Form Validation and Submission

From Dev

generic ajax form submission

From Dev

Ajax submission on page load

From Dev

How to show the image uploaded using HTML file control on a new page after form submission in PHP?

From Dev

Django redirect to index view with correct URL after form submission

From Dev

Rails/Ajax change form from create to update after submission

From Dev

Refresh Div's content after AJAX form submission

From Dev

Bootstrap dropdown not working after initial ajax form submission

From Dev

How do i hide a form after submission is succesfull with Ajax/Jquery

From Dev

get redirect_to and flash notices to work after ajax form submission

From Dev

jquery ajax requests are not responding on the new tab after the php form submission

From Dev

AJAX and php form inadvertently refreshes again after submission

From Dev

No response after form submission and page just auto reload

From Dev

Can't get rid of blank php page after submission form

Related Related

  1. 1

    Don't reload page after form submission using ajax and jquery

  2. 2

    Not able to refresh the partial view after a form submission from another partial view within it, using ajax in mvc razor

  3. 3

    Redirect after AJAX form submission

  4. 4

    Redirect after AJAX form submission

  5. 5

    Form submission using AngularJS/AJAX

  6. 6

    Using jQuery AJAX to call a php page on form submission

  7. 7

    jQuery - stop form submission from changing page using ajax

  8. 8

    Form Submission not working second time after submiting using Jquery and Ajax

  9. 9

    Ajax form submission on a form created using jquery

  10. 10

    php return to page after form submission

  11. 11

    Display the same page even after form submission

  12. 12

    redirecting to another page after form submission

  13. 13

    php return to page after form submission

  14. 14

    Modal form Submission to PHP using Jquery .ajax

  15. 15

    Ajax form submission issues

  16. 16

    Ajax Form Validation and Submission

  17. 17

    generic ajax form submission

  18. 18

    Ajax submission on page load

  19. 19

    How to show the image uploaded using HTML file control on a new page after form submission in PHP?

  20. 20

    Django redirect to index view with correct URL after form submission

  21. 21

    Rails/Ajax change form from create to update after submission

  22. 22

    Refresh Div's content after AJAX form submission

  23. 23

    Bootstrap dropdown not working after initial ajax form submission

  24. 24

    How do i hide a form after submission is succesfull with Ajax/Jquery

  25. 25

    get redirect_to and flash notices to work after ajax form submission

  26. 26

    jquery ajax requests are not responding on the new tab after the php form submission

  27. 27

    AJAX and php form inadvertently refreshes again after submission

  28. 28

    No response after form submission and page just auto reload

  29. 29

    Can't get rid of blank php page after submission form

HotTag

Archive