1

I try to post data to laravel controller using ajax, but still get response null. My achievement is to post form data and return error message or success message. I'm newbie in ajax and laravel framework please help me to solve the problem.

Here is the meta tag header:

<meta name="_token" content="{{ csrf_token() }}">

Here is the html form :

{{ Form::open(['id'=>'testimonial_form','url'=>URL::action("processing-give-testimonial"),'method'=>'POST']) }}
{{ Form::hidden('_method', 'PUT') }}
<div class="row marginbot-20">
    <div class="col-md-6 xs-marginbot-20">
        {{ Form::text('name',null, ['class'=>'form-control input-lg','placeholder'=>'Enter Name','id'=>'name']) }}
    </div>
    <div class="col-md-6">
        {{ Form::email('email',null, ['class'=>'form-control input-lg','id'=>'email','placeholder'=>'Enter email','id'=>'email']) }}
    </div>
</div>
<div class="row">
    <div class="col-md-12">
        <div class="form-group">
            <select name="subject" id="subject" class="form-control" require="required">
                <option value="ask">Ask Question / Information</option>
                <option value="testimonial">Give Feedback / Testimonial</option>
            </select>
        </div>
        <div class="form-group">
            {{ Form::textarea('message',null,['id'=>'message','class'=>'form-control','rows'=>'4','cols'=>'25','placeholder'=>'message','id'=>'message']) }}
        </div>
        <!-- {{ Form::submit('Send Message',['id'=>'btnContactUs','class'=>'btn btn-skin btn-lg btn-block']) }} -->
        {{ Form::button('Submit', ['class'=>'btn btn-skin btn-lg btn-block','id'=>'click']) }}          

    </div>
</div>
{{ Form::close() }}

Here is the ajax code :

$(function() {
    $.ajaxSetup({
        headers: {
            'X-XSRF-Token': $('meta[name="_token"]').attr('content')
        }
    });
});
$(document).ready(function() {
    $('#click').click(function() {
        var formData = {
            name        : $('#name').val(),
            email       : $('#email').val(),
            subject     : $('#subject').val(),
            message     : $('#message').val(),
        };
        $.ajax({
            type     : "POST",
            url      : "{{ URL::action('processing-give-testimonial') }}",
            data     : formData,
            beforeSend: function () {
                    alert('sure?');
                },
            success: function(data) {
                console.log(data);
            },
            error: function() {
                console.log('error');
            }
        });
    });
});

Here is the controller :

public function create()
{ 
    $inputs = array(
                'name' =>Input::get('name'),
                'email' =>Input::get('email'),
                'subject' =>Input::get('subject'),
                'message' =>Input::get('message')
            );

        //return "we reached here";
        return Response::json("success");
    /*if(Request::ajax()) {
        return Response::json('success', 200);
    } else {
        return Response::json('failed', 400);
    }*/

    /* if(Request::ajax()) {
          $data = Input::get('email');
          //print_r($data);die;
          if ($data != '') return Response::json('success',200);
          else return Response::json('failed',400);
        }*/
    /*
    $input = Input::get('name');
    //$input = Input::get('_token');
    if ($input == '') {
        return Response::json('failed',400); 
    }
    else {
        return Response::json('success',200);
    }*/
    //if(!empty($input)) return Response::json(['data'=>'success']);
    //else return Response::json('data',$input);


}

Here is the my route :

Route::post('give-testimonial',['uses'=>'TestimonialController@store','as'=>'processing-give-testimonial']);

Here is the filter.php :

Route::filter('csrf', function() {
$token = Request::ajax() ? Request::header('X-XSRF-Token') : Input::get('_token');
if (Session::token() != $token) {
    throw new Illuminate\Session\TokenMismatchException;
} });
6
  • Try using return Response::create() instead of return Response::json() Commented Aug 29, 2015 at 20:02
  • @KhanShahrukh : the return still null or empty. Commented Aug 30, 2015 at 5:01
  • do your error logs show anything? Commented Aug 30, 2015 at 5:32
  • @Derek , no everything is fine. no error on the js and controller. but still the return is empty. Commented Aug 30, 2015 at 12:05
  • You are routing to a store method which you haven't provided here. Commented Aug 30, 2015 at 12:10

1 Answer 1

1

I'm guessing your routing is wrong. Try changing this route:

Route::post('give-testimonial',['uses'=>'TestimonialController@store','as'=>'processing-give-testimonial']);

to this:

Route::post('give-testimonial',['uses'=>'TestimonialController@create','as'=>'processing-give-testimonial']);
Sign up to request clarification or add additional context in comments.

Comments

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.