0

I'm using a jquery plugin in my site. I want to validate a form first, Then send another data to controller,But doesn't through form. First validate is return a success feedback already. But when a second one is send, Doesn't have any feedback return. Here is my code

route.php

Route::controller('designs', 'DesignsController');

DesignsController.php

public function postTest() {
    $validator = Validator::make(Input::all(), Design::$rules);
    if ($validator->passes()) {

        $response = array(
            'status' => 'success'
        );
        return Response::json( $response );

    }
    $response = array(
        'status' => 'fail'
    );
    return Response::json( $response );
}
public function postSaveimage() {

    //Save Image complete return $success=1

    if($success)
     $response = array(
        'status' => 'save image success'
    );
    else
     $response = array(
        'status' => 'Fail'
    );
    return Response::json( $response );

}

jquery

$('#design_form').on( 'submit' ,function() {

             //Validate First form data
            $.post(
                $( this ).prop( 'action' ),
                {
                    "_token": $( this ).find( 'input[name=_token]' ).val(),
                    "category_id": $( '#form_category_id' ).val(),
                    "title": $( '#form_title' ).val(),
                    "user_id": $( '#form_user_id' ).val(),
                },
                function( data ) {
                    //if validate fail
                    if(data.status=='fail')
                    {
                        alert('data.status');
                    }
                    //if validate pass
                    else
                    {
                       //Sent second data
                        $.post("designs/saveimage", 
                        { 
                            "_token": $( this ).find( 'input[name=_token]' ).val(),
                            'base64_image': yourDesigner.getProductDataURL() 
                        }, function(data) {
                            if(data) {
                                alert(JSON.stringify(data));
                            }
                            else {

                                alert('fail!');
                            }
                        });
                    }
                },
                'json'
            );
            return false;
        });
3
  • Hi, what does var_export( $_POST ); say in your controller code. Commented Nov 7, 2014 at 3:56
  • Try to use Firebug to see the Ajax response.Then you will know what's the actual return you get from you second POST Commented Nov 7, 2014 at 4:05
  • It said 500 Internal Server Error "Illuminate\Session\TokenMismatchException" Guess i've to create new token right? Commented Nov 7, 2014 at 4:23

2 Answers 2

1
$('#design_form').on( 'submit' ,function() {

        var _this = this;
         //Validate First form data
        $.post(
            $( this ).prop( 'action' ),
            {
                "_token": $( this ).find( 'input[name=_token]' ).val(),
                "category_id": $( '#form_category_id' ).val(),
                "title": $( '#form_title' ).val(),
                "user_id": $( '#form_user_id' ).val(),
            },
            function( data ) {
                //if validate fail
                if(data.status=='fail')
                {
                    alert('data.status');
                }
                //if validate pass
                else
                {
                   //Sent second data
                    $.post("designs/saveimage", 
                    { 
                        "_token": $( _this ).find( 'input[name=_token]' ).val(),
                        'base64_image': yourDesigner.getProductDataURL() 
                    }, function(data) {
                        if(data) {
                            alert(JSON.stringify(data));
                        }
                        else {

                            alert('fail!');
                        }
                    });
                }
            },
            'json'
        );
        return false;
    });

Add new line:

var _this = this;

And in the second send:

"_token": $( _this ).find( 'input[name=_token]' ).val()
Sign up to request clarification or add additional context in comments.

1 Comment

THANK YOU!! Work like charms! And thanks for pointing out my mistake.I'm understand now.
0

on your second ajax post you have to indicate the host

if it is in a js file you have to specify the host

  $.post("http://www.host.com/public/designs/saveimage")

and if in view

  $.post("{{URL::to('designs/saveimage')}}");

you should check where does the post go and if the route is correct using firebug in firefox or the console in google chrome. .

1 Comment

Firebug said that TokenMismatchException. I think it's about how i pass a data with a token

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.