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 /**
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);
}
});
}
});
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
You should try to replace 'let name' into 'var name' then alert or console it.
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);
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.