Ajax in Laravel Search function is not working - ajax

right now it is not showing any data;
console:finished loading: GET "http://todolist.local/teachers/search?text=a".
i am trying to show result in tbody, when user types something in search.
Ajax code:
<script>
$(document).ready(function(){
$('#searchname').on('keyup', function(){
var text = $('#searchname').val();
$.ajax({
type:"GET",
url: 'teachers/search',
data: {text: $('#searchname').val()},
success:function(data){
$('tbody').html(data);
}
});
});
});
</script>
web.php:
Route::get('/search', 'TeachersController#ajaxsearch');
Search Controller:
public function ajaxsearch(){
$searchname = Input::get ( 'searchname' );
if($searchname != ""){
$teacher = Teacher::where ( 'efirst', 'LIKE', '%' . $searchname . '%' )->paginate(10);
return response()->json($teacher);
}
}
view:
<div class="input-group stylish-input-group">
<input type="text" id="searchname" name="searchname" class="form-control" placeholder="Search..." >
<span class="input-group-addon">
<button type="submit" class="btn btn-primary">
<span class="glyphicon glyphicon-search">Search</span>
</button>
</span>
</div>

This should do the trick.
HTML:
<div id="datasearch"></div>
JS:
$(function(){
$('#searchname').on('keyup', function(){
$.get('/teachers/search/'+$(this).val(), function(response){
$('#datasearch').html(response);
});
});
});
Controller:
public function ajaxsearch(string $value = null){
return $value ? Teacher::whereRaw("UPPER(efirst) LIKE '%".strtoupper($value)."%'")->paginate(10) : [];
}
Route:
Route::get('/teachers/search/{value?}', 'TeachersController#ajaxsearch');
VERSION AFTER CHAT
HTML:
<ul id="datasearch"></ul>
JS:
$(function(){
var $datasearch=$('#datasearch');
$('#searchname').on('keyup', function(){
$.get('/teachers/search/'+$(this).val(), function(teachers){
$datasearch.empty();
for (var i=0; i<teachers.length; i++){
$datasearch.append('<li>'+teachers[i].efirst+' edit</li>');
}
});
});
});
Controller:
public function ajaxsearch(string $value = null){
return $value ? Teacher::select('id','efirst')->whereRaw("UPPER(efirst) LIKE '".strtoupper($value)."%'")->offset(0)->limit(10)->get() : [];
}
Route:
Route::get('/teachers/search/{value?}', 'TeachersController#ajaxsearch');

do you know about error function in jquery ajax?
$(document).ready(function(){
$('#searchname').on('keyup', function(){
var text = $('#searchname').val();
$.ajax({
type:"GET",
url: 'teachers/search',
data: {text: $('#searchname').val()},
success:function(data){$('tbody').html(data);},
error:function(jqXHR){alert(jqXHR.status);}
});
});
});
You can try this,it will show error if there is some and text should be in inverted commas as it is a key value pair.

In your search controller you are accessing wrong input name. It should be like this
public function ajaxsearch(){
$searchname = Input::get ( 'text' );
if($searchname != ""){
$teacher = Teacher::where ( 'efirst', 'LIKE', '%' . $searchname . '%' )->paginate(10);
return response()->json($teacher);
}
}

First of all, just go to the URL manually and enter parameters.
http://todolist.local/teachers/search?text=a
If you get any response. It's mean your PHP working good.
If you get an error, you are using the GET method. Please pass the variable argument in Route
Route::get('/search/{searchName}', 'TeachersController#ajaxsearch');
and please correct your controller
public function ajaxsearch($searchname){
if($searchname != ""){
$teacher = Teacher::where ( 'efirst', 'LIKE', '%' . $searchname.'%' )->paginate(10);
return response()->json($teacher);
}
}
Second, Please $.get method in Jquery for GET Method AJAX
<script>
$(document).ready(function(){
$('#searchname').on('keyup', function(){
var text = $('#searchname').val();
$.get(urlHere, function(response){
console.log(response);
});
});
});
</script>
See your console tab, If you get response

Related

Laravel Controller/Ajax not saving in my database

It seems like my save(); in my categories does not function as intended below. I will show the necessary codes first:
my table name is hms_bbr_category which is also connectec to my .env locally:
DB_CONNECTION=pgsql
DB_HOST=localhost
DB_PORT=5432
DB_DATABASE=jhs
DB_USERNAME=postgres
DB_PASSWORD=pa55wor0
my model: HmsBbrCategory
class HmsBbrCategory extends Model
{
protected $table = 'hms_bbr_category';
protected $fillable = [
"category_name", "category_description"
];
}
my controller: BBRCategoryConfigurationController
class BBRCategoryConfigurationController extends Controller
{
public function index(){
return view('frontend.bbr-settings.bbr-category-configuration');
}
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'category_name'=>'required|max:191',
'category_description'=>'required|max:191',
]);
if($validator->fails())
{
return response()->json([
'status'=>400,
'errors'=>$validator->messages(),
]);
}
else {
$category = new HmsBbrCategory;
$category->category_name = $request->input('category_name');
$category->category_description = $request->input('category_description');
$category->save();
return response()->json([
'status'=>200,
'message'=>'Category Added!',
]);
}
}
The ajax and modal fields
<div class="form-group">
<input type="text" class="form-control form-group w-100 category_name" placeholder="Category Name">
</div>
<div class="form-group">
<textarea class="form-control w-100 category_description" placeholder="Category Description" cols="50" rows="10"></textarea>
</div>
<script>
$(document).ready(function (){
$(document).on('click', '.add_category', function(e){
e.preventDefault();
var category_data = {
'category_name': $('.category_name').val(),
'category_description': $('.category_description').val(),
}
//token taken from laravel documentation
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
console.log(category_data);
$.ajax({
type: "POST",
url: "/clinical/bbr-category-configuration",
data: "category_data",
dataType: "json",
success: function (response){
// console.log(response);
if(response.status == 400)
{
$('#saveform_errList').html("");
$('#saveform_errList').addClass('alert alert-danger');
$.each(response.errors, function (key, err_values) {
$('#saveform_errList').append('<li>'+err_values+'</li>');
});
}
else
{
$('#saveform_errList').html("");
$('#success_message').addClass('alert alert-success');
$('#success_message').text(response.message);
$.('#createCategory').modal('hide');
$.('#createCategory').find('input').val("");
console.log(category_data);
}
}
});
});
});
</script>
my routes at web.php
Route::get('/bbr-category-configuration', [BBRCategoryConfigurationController::class,'index']);
Route::post('/bbr-category-configuration', [BBRCategoryConfigurationController::class,'store']);
Things to note:
my hunch is that my store function does not connect properly at $category = new HmsBbrCategory; However I have checked that my table name and the fields taken are the same, as seen at $category->category_name = $request->input('category_name');
I have also tested in ajax with the values by simply adding console.log(response) as seen in the screenshot, I cannot get past my validator to get to the save(). I am not sure how but There should not be an error since my text fields are filled.
I can elaborate more if needed, I am asking what can I change to fix my validation/save. thanks for any help.
As the error shows, The validation is failing (empty value i guess) and returning the code you programmed (400).
i'm guessing it is because you are using a string instead of the variable at the attribute data: "category_data",
update the code to send the variable instead
$.ajax({
type: "POST",
url: "/clinical/bbr-category-configuration",
data: category_data, //change here
dataType: "json",
success: function (response){
//...

How to increment a model attribute count?

I'm trying to make a very simple blog using Laravel and Ajax, each blog post has likes so i want to increment the likes by clicking and updating the DB also in console there is no errors.
this is my schema :
Schema::create('blogs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->longText('blogContent');
$table->timestamps();
$table->integer('likes')->default(8);
});
here's my blade template and script :
#section('content')
<div class="myFeedContainer col-lg-9 col-md-12 col-sm-12">
#foreach ($blogs as $blog)
<div class='blogContainer'>
<h5 class='blogTitle'>{{$blog->title}}</h5>
<h6 class='blogDate'>{{$blog->created_at}}</h6>
<p class='blogContent1' >{{$blog->blogContent}}</p>
<hr>
<h6 class='blogLikes' class="clickforlikes" data-id='{{$blog->id}}'>
{{$blog->likes}}
<ion-icon name="heart" ></ion-icon>
</h6>
<br>
</div>
#endforeach
<div id='pagina'>{{ $blogs->render() }}</div>
<script>
$('.clickforlikes').on("click", function(){
$.ajax({
url:'updateLikes',
type: 'POST',
data: {
blog_id: $(this).attr('data-id')
},
success: function (data){
console.log(data);
},
error: function(request, stqtus, error){
console.log('code: ' + request.status + '\n' + 'msg: '+request.responseText+'\n'+'error: ' + error);
}
});
});
</script>
</div>
#endsection
And this is the function from my controller
public function updateLikes()
{
$blog_Id = $_POST['blog_id'];
$blog = Blog::find($blog_Id)->increment('likes');
$blog->save();
}
This is the router :
Route::post('/updateLikes', 'BlogsController#updateLikes');
You are trying to accomplish this the PHP way. Since you are using a framework, you should take advantage of its cool features. In Laravel, you would have to do something like this:
Router:
Route::put('updateLikes/{id}', 'BlogsController#updateLikes');
Controller:
public function updateLikes($id) { $blog = Blog::find($id)->increment('likes'); $blog->save(); }
Ajax call:
<script> $('.clickforlikes').on("click", function(){ $.ajax({ url:'updateLikes/'+$(this).attr('data-id'), type: 'PUT', success: function (data){ console.log(data); }, error: function(request, stqtus, error){ console.log('code: ' + request.status + '\n' + 'msg: '+request.responseText+'\n'+'error: ' + error); } }); }); </script>
The HTTP method is different because a POST request is meant to be used to create a new resource in the database. On the other hand, PUT is used to update a resource. Your ajax request will call the route with the id of the blog appended to it, and the controller will get the id as a parameter and perform the update.
You are using POST method. You can receive these data by Request Service Container in updateLikes() method.
public function updateLikes(Request $request)
{
$blog_Id = $request->get('blog_id');
$blog = Blog::find($blog_Id)->increment('likes');
// other codes if needed
}

Dynamic dependent select option using AJAX and Laravel 5.8

I am actually trying to obtain a dynamic dependent select option for city based on region and in my controller i am returning a string(using echo) but unfortunately the string is not rendering in the select option(of cities) on the browser. but i can see it by inspection or by printing on the console.
my jquery:
<script >
$(document).ready(function(){
$('.dynamic').change(function(){
if($(this).val() != '')
{
var select = $(this).attr("id");
var value = $(this).val();
var dependent = $(this).data('dependent');
var _token = $('input[name="_token"]').val();
$.ajax({
url:"{{ route('dynamicdependent.fetch') }}",
method:"POST",
data:{select:select, value:value, _token:_token, dependent:dependent},
success:function(result)
{
$('#cityName').html(result);
console.log(result);
console.log(result);
}
})
}
});
$('#RegionName').change(function(){
$('#cityName').val('');
});
});
</script>
: my controller
class DynamicDependent extends Controller
{
public function fetch(Request $request)
{
$select = $request->get('select');
$value = $request->get('value');
$dependent = $request->get('dependent');
$data = city::where($select,$value)->get();
$output = '<option value="">Select '.ucfirst($dependent).'</option>';
foreach($data as $row)
{
$output .= '<option value="'.$row->$dependent.'">'.$row->$dependent.'</option>';
}
echo $output;
}
}
:no eroor message
Here is the picture of the actual outpout :
I suspect that the response might not be that well formed.
Try to return a proper response instead of echoing yourself.
// other code in the controller ...
return response()->json($output);
If it doesn't help, please include the actual response you get in the browser/console.
thanks. actually i was using bootstrap select library and there is a class called selectpicker when i removed this class from the select tag of city it worked fine

Post 403 forbidden on Yii2 Ajax Call

I know there's other topic on this in stackoverflow but it still didn't work for me. I try to make a simple dynamic menu that generate dynamic content based on the chosen <li> id.
This is the code to generate the menu :
foreach($cabang as $index=>$model){
echo '<li id='.$model->idDpd->id_dpd.'>
<a class="nav-link" href="#" role="tab" data-toggle="tab">'.$model->idDpd->dpd.'</a>
</li>';
}
The menu is created successfully. But I have the problem with the content generated with Ajax
This is what I have in my view file :
$script = <<< JS
$(document).ready(function(){
function load_page_details(id)
{
$.ajax({
url: "<?=Url::to(['/site/fetch']) ?>",
method:"POST",
data:{id:id}, //pass the 'id' of Load_page_details function parameter to the targeted URL
success:function(data)
{
$('#page_details').html(data);
}
});
}
//load page-details where the id in the database table equals 1. Set the default to 1 while page is loading for the first time.
/* load_page_details(1);*/
$('.nav li').click(function(){
var page_id = $(this).attr("id");
load_page_details(page_id);
});
});
JS;
$this->registerJs($script)
?>
This is my SiteController and the action :
public function actionFetch(){
if (Yii::$app->request->isAjax) {
// fetch the $_POST["id"]
$data = Yii::$app->request->post('id');
if(isset($data))
{
$query= Cabang::find()->where(['id_dpd'=> $data])
->joinWith('idDpd')
->all();
$output = '';
foreach($query as $model)
{
$output .= '
<div role="tabpanel" class="col-lg-4 tab-pane fade show active" >
<div class="col-md-12">
<h4>'.$model->kota.'</h4>
<p>'.$model->alamat.'</p>
<p>'.$model->telp.'</p>
<p>'.$model->email.'</p>
<p>'.$model->jadwal.'</p>
</div>
</div>
';
}
/* echo $output;*/
// return Json
return \yii\helpers\Json::encode($output);
}
}
}
The error caught in my console in chorome dev tool : jquery.js:9175 POST http://localhost/%3C?=Url::to([%27/site/fetch%27])%20?%3E 403 (Forbidden)
I tried to make the fetch function to a new php file and link the URL in my Ajax to that file (not to a controller or SiteController in my case) like : url: url:"site/fetch.php",but it returned jquery.js:9175 POST http://localhost/site/fetch.php 404 (Not Found)
What am I doing wrong? I have spent two days without solution. Thanks for the help!
Your PHP is not correct - you cannot use <?= /* ... */ ?> inside of heredoc. You need to use temporary variable:
$url = Url::to(['/site/fetch']);
$script = <<<JS
$(document).ready(function () {
function load_page_details(id) {
$.ajax({
url: "$url",
method: "POST",
data: {id: id}, //pass the 'id' of Load_page_details function parameter to the targeted URL
success: function (data) {
$('#page_details').html(data);
}
});
}
$('.nav li').click(function () {
var page_id = $(this).attr("id");
load_page_details(page_id);
});
});
JS;
$this->registerJs($script);

Why the if condition does not filtering the results?

I want to create advance search filtering staff data according to certain criteria chosed by users. This application developed using laravel 5. I am querying the data using ajax function and if statement to filter the criteria. The results appear but it does not filter any condition in the if statement.
The controller of the filtering condition is this:
public function kakitangan(Request $request)
{
$query = DB::table('itemregistrations')
->select('itemregistrations.ItemRegistrationID','itemregistrations.name', 'itemregistrations.Nobadan');
if ($request->section != ""){
$query->where('SectionID', $request->section);
}
$newitem = $query->get();
return response::json($newitem);
}
I also have tried this:
$query = DB::table('itemregistrations')
->select('itemregistrations.ItemRegistrationID','itemregistrations.name', 'itemregistrations.Nobadan');
if(request('section')) {
$query->where('SectionID', request('section'));
}
$newitem = $query->get();
return response::json($newitem);
But the result is the same..all data in itemregistrations appear in the output page. Although I select another section criteria.
This is the view page code for selection:
<div class="row">
<div class="col-lg-2">
{{ Form::label('Seksyen', 'Seksyen') }}
</div>
<div class="col-lg-2">
{{ Form::select('section', $sections, '', ['class' => 'form-control select2', 'placeholder' => '--pilih--']) }}
</div>
</div>
</div>
The selection id is from controller function:
$sections = Section::pluck('sectionname', 'SectionID');
//sent to html view
return view('carian.index', compact('sections'));
Button to call ajax function to get the query:
<button class="btn btn-primary btn-md" id="cari">Cari</button>
The code for results appear:
<script type="text/javascript">
$( "#cari" ).click(function() {
var seksyen = $("#section").val();
$.ajax({
url: '{{ url('kakitangan') }}',
data: {'section': seksyen},
dataType: 'json',
success: function (data) {
console.log(data);
$('#datatable tr').not(':first').not(':last').remove();
var html = '';
for(var i = 0; i < data.length; i++){
html += '<tr>'+
'<td>' + data[i].name + '</td>' +
'</tr>';
}
$('#datatable tr').first().after(html);
},
error: function (data) {
}
});
});
</script>
Should be when the user select a section, only staffs of the section appear. But now all staffs appear when select any section.
I just tried to test whether the section value is correctly passed to the controller using this in controller:
$try=$request->input('section');
return response::json($try);
It results empty array..no value passed? Is it the section value is not passed correctly? How to correct this problem?
You are passing the section as a post param while you performing a GET request.
Using jQuery you can send this as a query string using:
var seksyen = $("#section").val();
$.ajax({
url: '{{ url('kakitangan') }}?' + $.param({'section': seksyen}),
dataType: 'json',
...
In your controller you can also explicitly check if a request contains a query string using the has method on a request
if(request()->has('section')) {
$query->where('SectionID', request('section'));
}
EDIT:
using the laravel collective Form helpers you can specific the field id using the following (note the fourth argument id)
{{ Form::select('section', $sections, '', ['id' => 'section', 'class' => 'form-control select2', 'placeholder' => '--pilih--']) }}

Resources