0

First of all, I am a beginner in using ajax inside laravel. I tried to check the input field while it's working or not through an alert or console.log it's returning undefined.

my blade file code

<div id="add_tag_modal" class="modal fade">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-body">
                <h2>Add New Tag</h2>
                <hr>
                <form id="add_tag_form" method="POST">
                    @csrf
                    <div class="mess"></div>
                    <div class="form-group">
                        <label for="">Name</label>
                        <input name="name" type="text" class="form-control">
                    </div>
                    <div class="form-group">
                        <input class="btn btn-primary btn-sm" type="submit" >
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

my controller file code..though its commented public function store(Request $request) { // Tag::create([

    //     'name' => $request -> name

    // ]);
}

web.php /**

  • tag controller */

Route::resource('tag','App\Http\Controllers\TagController');

Route::post('tag-create','App\Http\Controllers\TagController@store')->name('tag.create');

lastly my ajax code

  $(document).on('submit','form#add_tag_form',function(e){

    e.preventDefault();


    let name = $('form#add_tag_form input[name="name"]').val();


    if (name == '') {

        $('.mess').html('<p class="alert alert-danger">All fields are required!<button type="button" class="close" data-dismiss="alert" aria-label="Close">');

    }else{

         $.ajax({
    url:'tag-create',
    method: "POST",
    contentType: false,
    processData : false,
    data : new FormData(this),
    success: function(data){
        console.log(data.name);

    }
         });
    }

  });

3 Answers 3

0

I believe you need to update your controller.

You need to validate the request first

$validate = $this->validate($request, [
    'name' => 'required|string|max:255'
]);

after that, send the $validate variable to model Tag to create a new resource:

$save = Tag::create($validate);

if($save) {
   return true; //return statements you can customize based on your requirement
} 
return false;

By doing this you are validating the incoming request and if your return value is true process further. But be carefull with response you should return the message and status code to validate the response.. Don't use true or false for return statement. In AJAX response, you check the status code and proceed further with the message.

Thank you

Sign up to request clarification or add additional context in comments.

5 Comments

after doing this....i check console and it says 500 Internal Server Error.. which was the same result before editing my code
What message do you get from the error. You can check the error message in chrome dev tools or by clicking control+shift+J on windows, and under network tab, click on the route with error
it says... POST localhost:8000/tag-create 500 (Internal Server Error)
What was the response message??
When you click on the network tab you see the error route click on that, another new left tab will open. Then navigate to the response to see the error message you are getting from laravel
0

You should try to replace 'let name' into 'var name' then alert or console it.

Comments

0

You can try adding this to your php code to show the error message of the 500 code.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

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.