My purpose is when a user order a product need to get a notification in the dashboard page. Now the notification is getting . when i click Ok, let me check button in the dashboard its go to order listing page. but the problem is when i back to the dash baord the same order notication will coming. i need once i click, when i click Ok, let me check button the same order pop-up will not show
my Ajax
<script>
setInterval(function () {
$.get({
url: '{{route('get-order-data')}}',
dataType: 'json',
success: function (response) {
let data = response.data;
if (data.new_order > 0) {
playAudio();
$('#popup-modal').appendTo("body").modal('show');
}
},
});
}, 1000);
function check_order() {
location.href = '{{route('admin-order-pending')}}';
}
Function in Laravel
public function order_data()
{
$new_order = DB::table('orders')->where(['checked' => 0])->count();
return response()->json([
'success' => 1,
'data' => ['new_order' => $new_order]
]);
}
Pop-up code in dashboard
<div class="modal" id="popup-modal">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-body">
<div class="row">
<div class="col-12">
<center>
<h2 style="color: rgba(96,96,96,0.68)">
<i class="tio-shopping-cart-outlined"></i> You have new order, Check Please.
</h2>
<hr>
<button onclick="check_order()" class="btn btn-primary">Ok, let me check</button>
</center>
</div>
</div>
</div>
</div>
</div>
try this..
<script>
setInterval(function () {
$.get({
url: '{{route('get-order-data')}}',
dataType: 'json',
success: function (response) {
let data = response.data;
if (data.new_order > 0) {
playAudio();
$('#popup-modal').appendTo("body").modal('show');
}
$('#popup-modal').appendTo("body").modal('none');
},
});
}, 1000);
function check_order() {
location.href = '{{route('admin-order-pending')}}';
}
Function in Laravel
public function order_data()
{
$new_order = DB::table('orders')->where(['checked' => 0])->count();
return response()->json([
'success' => 1,
'data' => ['new_order' => $new_order]
]);
}
Pop-up code in dashboard
<div class="modal" id="popup-modal">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-body">
<div class="row">
<div class="col-12">
<center>
<h2 style="color: rgba(96,96,96,0.68)">
<i class="tio-shopping-cart-outlined"></i> You have new order, Check Please.
</h2>
<hr>
<button onclick="check_order()" class="btn btn-primary">Ok, let me check</button>
</center>
</div>
</div>
</div>
</div>
</div>
enter code here
Related
I'm writing a Laravel app and trying to implement a modal showing some details on an element. When I'm clicking on the link the backdrop is showing but not the modal window. In the network monitor of chrome it is showing the modal window in preview.
What's wrong?
Thank you in advance for your help!
Here is my code
index.blade.php
<td>
Details
</td>
modal.blade.php
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">#yield('title')</h5>
</div>
<div class="modal-body">
#yield('content')
</div>
<div class="modal-footer">
#yield('footer')
</div>
</div>
</div>
show.blade.php
#extends('layouts.modal')
#section('title')
Demo Modal
#endsection
#section('content')
<p>test</p>
#endsection
#section('footer')
<button type="button" data-dismiss="modal">Close</button>
#endsection
remote.js
$(document).on('ajax:success', function(e, xhr){
if(!$('#modal').length){
$('body').append($('<div class="modal" id="modal"></div>'))
}
$('#modal').html(xhr.responseText).modal('show');
});
ProjectController.php
public function show($id)
{
$project = Project::findOrFail($id);
return view('projects.show', compact('project'));
}
Hey never use HTML Element in making a clickable modals that has value on it.
I suggest you used add value on it , That can be "ID" then add modal attributes on it, Then boom everything is fine and working.
You need to specify the html id tag in your link.
I suggest you try this
<a data-toggle="modal" id="smallButton" data-target="#smallModal"
data-attr="{{ route('projects.show', $project->id) }}" title="show">
<i class="fas fa-eye text-success fa-lg"></i>
</a>
<!-- small modal -->
<div class="modal fade" id="smallModal" tabindex="-1" role="dialog" aria-labelledby="smallModalLabel"
aria-hidden="true">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="smallBody">
<div>
<!-- the result to be displayed apply here -->
</div>
</div>
</div>
</div>
</div>
<script>
// display a modal (small modal)
$(document).on('click', '#smallButton', function(event) {
event.preventDefault();
let href = $(this).attr('data-attr');
$.ajax({
url: href,
beforeSend: function() {
$('#loader').show();
},
// return the result
success: function(result) {
$('#smallModal').modal("show");
$('#smallBody').html(result).show();
},
complete: function() {
$('#loader').hide();
},
error: function(jqXHR, testStatus, error) {
console.log(error);
alert("Page " + href + " cannot open. Error:" + error);
$('#loader').hide();
},
timeout: 8000
})
});
</script>
In your Remote.js file :
$(document).on('ajax:success', function(e, xhr){
if(!$('#modal').length){
$('body').append($('<div class="modal" id="modal"></div>'))
}
$('#modal').modal('show');
$('#modal').html(xhr.responseText);
});
I have 2 ajax calls, 1 for create and 1 for delete a folder, here is the source code
<!-- Create folder ajax -->
<script>
$(document).ready(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('input[name="_token"]').val()
}
});
$('#create-folder').click(function(e) {
e.preventDefault();
$.ajax({
data: $('#create-folder-form').serialize(),
url: "{{ route('folders.store') }}",
type: "POST",
dataType: "json",
success: function (data) {
if(data.errors) {
alert('Sorry');
} else {
$('#create-folder-form').trigger("reset");
$('#exampleModal').modal('hide');
$('.folders').append('<li data-id=' + data.id + '>' + data.name + " <button type='button' class='btn btn-danger delete-folder' data-toggle='modal' data-id='" + data.id + "'> Delete " + "</button>" + '</li>');
}
},
error: function (data) {
console.log('Error ' + data);
}
})
});
// Delete ajax call
$('.delete-folder').click(function(e) {
let id = ($(this).data('id'));
$.ajax({
type: 'delete',
url: "folders/" + id,
success: function(data) {
$('.folders li[data-id=' + data.id + ']').remove();
}
});
});
});
</script>
index.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
#include('partials.nav')
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Folders</div>
<div class="card-body">
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Create folder
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Create</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="create-folder-form">
#csrf
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" id="name">
</div>
<div class="form-group">
<button type="button" class="btn btn-primary" id="create-folder">Create</button>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row justify-content-center mt-3">
<div class="col-md-8">
<div class="card">
<div class="card-header">Folders</div>
<div class="card-body">
#if($folders)
<ul class="folders">
#foreach($folders as $folder)
<li data-id="{{ $folder->id }}">
{{ $folder->name }}
<button type='button' class='btn btn-danger delete-folder' data-toggle='modal' data-id='{{ $folder->id }}'>Delete</button>
</li>
#endforeach
</ul>
#endif
</div>
</div>
</div>
</div>
</div>
#endsection
Controller
public function index()
{
$user = User::findOrFail(Auth::id());
$folders = $user->folders()->get();
return view('folders.index', compact('folders'));
}
public function store(Request $request)
{
$attributes = $request->validate([
'name' => 'required'
]);
$attributes['user_id'] = Auth::id();
$folder = Folder::create($attributes);
return Response::json($folder);
}
public function destroy(Folder $folder)
{
$folder->delete();
return Response::json($folder);
}
Everything works well, it adds me the element to the DOM, but there is a little problem, when I am trying to delete a folder after i create it, it does not work, I need to refresh my page and then it is working.
Easy mistake to make. jQuery can't find the dynamically added button directly.
Instead of
$('.delete-folder').click(function(e) {...});
use
$('body').on('click','.delete-folder', function(e) {...});
I have 2 LARAVEL paginations on one page, know i want to implement AJAX technology in order to achive paginations without reload of entire page. With one pagination ajax works perfectly but i have problems when i want to implement AJAX on second Laravel pagination. Does anyone can help me ?
First pagination
Second pagination
Controller
function successlogin(Request $request)
{
$posts= Post::orderby('created_at', 'desc')->paginate(3, ['*'], 'page_1s');
$documents= Document::orderby('created_at', 'desc')->paginate(2, ['*'], 'page_2s');
if ($request->ajax()) {
return view('presult', compact('posts'));
}
return view('main_page', ['posts'=>$posts,'documents'=>$documents]);
}
main_page
<section id="services">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2 class="section-heading" style="color: #d21826; font-weight: bold;">Vijesti</h2>
<hr class="my-4">
</div>
</div>
</div>
<div class="container">
#if(session('deletePost'))
<div class="alert alert-danger" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
{{ session('deletePost') }}
</div>
#endif
<div id="tag_container">
#include('presult')
</div>
</div>
</section>
<section class="bg-primary" id="about">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2 class="section-heading" style="color: white; font-weight: bold;">Službeni Dokumenti</h2>
<hr class="light my-4">
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-lg-12 mx-auto text-center">
<div id="table_container" class="table-responsive">
#include('table')
</div>
</div>
</div>
</div>
#if (Auth::user())
Dodaj Vijest
#endif
</section>
ajax_script for pagination
$(window).on('hashchange', function() {
if (window.location.hash) {
var page = window.location.hash.replace('#', '');
if (page == Number.NaN || page <= 0) {
return false;
}else{
getData(page);
}
}
});
$(document).ready(function()
{
$(document).on('click', '.pagination a',function(event)
{
event.preventDefault();
$('li').removeClass('active');
$(this).parent('li').addClass('active');
var myurl = $(this).attr('href');
var page=$(this).attr('href').split('page_1s=')[1];
getData(page);
});
});
function getData(page){
$.ajax(
{
url: '?page_1s=' + page,
type: "get",
datatype: "html"
})
.done(function(data)
{
$("#tag_container").empty().html(data);
location.hash = page;
})
.fail(function(jqXHR, ajaxOptions, thrownError)
{
alert('No response from server');
});
}
Hi guys I am having problem with my ajax passing postID to my modal . var pid gets the id I wanted but when I echo the postID it doesnt get the value from ajax. please help
$(document).ready(function(){
$('#myModal').on('show.bs.modal', function (e) {
var pid = $(e.relatedTarget).data('id');
$.ajax({
type: 'post',
url: 'domain/modal.php',
data: 'post_id=' + pid,
success: function (r) {
}
});
});
});
<a class="link-to-post" data-toggle="modal" data-target="#myModal" name="modal" data-id="<?php echo get_the_ID(); ?>">Button</a>
<?php
$post_ID = $_POST['post_id'];
?>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-body">
<div class="row">
<div class="col-sm-6">
<?php echo $post_ID;?>
</div>
<div class="col-sm-6">
</div>
</div>
</div>
</div>
</div>
</div>
Can you try this code?
change data: 'post_id=' + pid,
to data:{ 'post_id': pid },
add an id in the div where you want to put your id. Something like:
<div id="idContainer" class="col-sm-6"></div>
in your success function you could add this
$('#idContainer').html(pid);
I am new to laravel and even more with ajax, I am trying to update from ajax but I can not do anything, it stays frozen and after a few seconds the url is updated with the data that happens to it, but in the base data nothing happens. If you could correct the code I would appreciate it because I am sure that this is the one that is giving problem because of my lack of experience, thank you very much
Route:
Route::name('appointments.update')->put('/appointment/$id', 'AppointmentController#update');
html:
<div class="modal fade" id="UpdateEventModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel">Update</h4>
</div>
<form class="form-horizontal">
<div class="modal-body">
#include('appointments._form-appointments')
</div>
<div class="modal-footer">
<div class="col-md-12 form-group">
{{ Form::submit('Update',['class' => 'btn btn-primary','id' => 'btn-modal-update',]) }}
{{ Form::button('Close',['class' => 'btn btn-default', 'data-dismiss' => 'modal']) }}
</div>
</div>
</form>
</div>
</div>
ajax:
$(document).ready(function() {
$('#btn-modal-update').click(function(){
var id = $('#appointment_id').val();
var route="{{url('appointments.update')}}/"+id+"";
$.ajax({
type:'PUT',
headers:{"Authorization": localStorage.getItem('token')},
url:route,
dataType:'json',
data: {startTime: $('input#startTime').val(), finalTime: $('input#finalTime').val(), patient_id: $('select[name="patient_id"').val(),
user_id: $('select[name="user_id"').val(event.user_id), date: $('input#date').val(), appointment_id: $('#appointment_id').val()},
success:function(data){
if(data.success == 'true'){
alert('updated');
}
},
error:function(data){
alert('not updated');
}
});
});
});
controller:
public function update(Request $request, $id)
{
if($request->ajax()){
$appointment = Appointment::find($id);
$appointment->date = $request->date;
$appointment->startTime = $request->startTime;
$appointment->finalTime = $request->finalTime;
$appointment->clinic_id = $request->clinic_id;
$appointment->user_id = $request->user_id;
$appointment->patient_id = $request->patient_id;
$result = $appointment->save();
if (result) {
return response()->json(['success'=>'true']);
}
else{
return response()->json(['success'=>'false']);
}
}
}
You should change your event listener a little bit:
Listen to the submit() event on your form and make sure that you prevent the normal form submission by adding the following line at the end of the callback: return false;
If it still isn't doing anythin, check your console for error output.