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.
Related
I tried to pass data from controller to ajax but data didnt come to my modal
This My Controller :
public function grd_checkout($paket)
{
$data= Voucher::where([
['paket',$paket],
['lokasi','GRD.NET']
])->count();
return response()->json([
'stok' => $data,
]);
}
This My Route :
Route::get('menu/grd_checkout/{paket}', [MenuController::class, 'grd_checkout']);
Button :
<td align="center" >
<button type="button" class="btn btn-primary" data-id="{{ $voucher->paket }}"
data-bs-toggle="modal" data-bs-target="#myModal">Beli
</button>
</td>
This My Form Modal :
<div class="modal fade" id="myModal" tabindex="-1" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content modal-sm">
<div class="modal-header">
<h5 class="modal-title" id="myModalLongTitle">Isi Data Diri dan Jumlah Pemesanan</h5>
<button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="col-sm-6">
Stok Voucher Tersedia : <p id="stok"></p>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success">Submit</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
This My Ajax :
<script>
$('#myModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
let paket = $(this).data('id');
$.ajax({
url: '/menu/grd_checkout/' + paket,
type: 'GET',
success: function(data) {
$('#stok').text(data.stok);
}
});
});
</script>
Please kinda help to solve my error, the "stok" didnt shown up in modal, Thanks for helping!
Kindly check the response of the API and if it's okay then kindly try this:
<script>
$('#myModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
let paket = $(this).data('id');
$.ajax({
url: '/menu/grd_checkout/' + paket,
type: 'GET',
success: function(data) {
$(document).find('#stok').html(data.stok);
}
});
});
</script>
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
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 want to open model on successful submission of form but although form is submitting modal is not popped up.
public function insert($data) {
// Inserting into your table
// Calling model
$done = $this->db->insert('sign_up', $data);
if($done) {
echo "<script>$('#thankyouModal').modal('show')</script>";
echo '<div class="modal fade" id="thankyouModal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Thank you for pre-registering!</h4>
</div>
<div class="modal-body">
<p>Thanks for getting in touch!</p>
</div>
</div>
</div>
</div>';
}
}
I am trying to open the model in controller using
$data = array(
'first_name' => $fname,
'last_name' => $lname,
'email' => $email,
'password' => $password
);
$this->load->model('modal');
$this->modal->insert($data);
Try below code; this will work
public function insert($data) {
// Inserting into your table
// Calling model
$done = $this->db->insert('sign_up', $data);
// You can do something else here
if($done) {
?>
<script>$(document).ready(function(){$('#thankyouModal').modal('show')});</script>
<div class="modal fade" id="thankyouModal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Thank you for pre-registering!</h4>
</div>
<div class="modal-body">
<p>Thanks for getting in touch!</p>
</div>
</div>
</div>
</div>
<?php
}
}
Put the code of modal in a view file so that we can easily call the modal with javascript in the view file.
And you need to set a variable in flash data so that you can check the condition that the form has been succesfully inserted or not.
Here is the code , I hope it helps you:
Controller file :
public function insert($data) {
// Inserting into your table
// Calling model
$done = $this->db->insert('sign_up', $data);
// You can do something else here
if($done) {
//You can set the message and variable name as per your need.
$this->session->set_flashdata('inserted','Yes');
//Redirect to the desired view file
redirect("controller/anotherfunction_where_view_file_is_loaded");
}
View File:
<div class="modal fade" id="thankyouModal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Thank you for pre-registering!</h4>
</div>
<div class="modal-body">
<p>Thanks for getting in touch!</p>
</div>
</div>
</div>
</div>
<?php if((isset($this->session->flashdata('inserted'))) && $this->session->flashdata('inserted') != "") { ?>
<script type="text/javascript">
$(document).ready(function(){
$('#thankyouModal').modal('show');
});
</script>
<?php } ?>
I got the answer
CONTROLLER
public function insert() {
$signup_email = $this->input->post('signup_email');
$signup_password = $this->input->post('signup_password');
// Checking if everything is there
if ($fname && $signup_email && $signup_password) {
$data = array(
'email' => $signup_email,
'password' => $signup_password
);
if($this->modal->insert($data)) {
echo "SOME DATA TO BE DISPLAYED IN MODAL";
}
}
}
VIEW
<body>
<div class="modal fade" id="thankyouModal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Thank you for pre-registering!</h4>
</div>
<div class="modal-body">
<p>Thanks for getting in touch!</p>
</div>
</div>
</div>
</div>
<script>
var signup_email = $('#signUp').val();
var signup_password = $('#signUpPassword').val() ;
$.ajax({
type: "POST",
url: "<?php echo site_url('form/insert'); ?>",
data: { signup_email: signup_email, signup_password: signup_password },
dataType: "html",
success: function(data) {
if(data) {
$('#thankyouModal').modal('show');
}
}, error: function() {
alert("ERROR!");
}
});
</script>
</body>
I am trying to show a bootstrap modal on body load based on value from mysql database. I have included bootstrap modal in body and showing it successfully according to database value with:
$resultsPop = mysql_query("select popup from members where mid=" . $mid);
$pop = mysql_result($resultsPop, 0, "popup");
if($pop == 0) {
$popupval = "<script type='text/javascript'>$(window).load(function(){ $('#MyPopUp').modal('show'); });</script>";
} else {
$popupval = "";
}
echo $popupval;
And my modal code is:
<div class="modal fade" id="MyPopUp" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="">
<div class="modal-dialog modal-lg">
<div class="modal-content" style="">
<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">Welcome</h4>
</div>
<div class="modal-body" style="text-align: center;">
<span>
Welcome Popup
</span>
</div>
</div>
</div>
</div>
Now, I want to have a checkbox in the modal which on checked can silently pass value 1 and logged in member variable $mid value to a php page after user clicks on modal close button. So, popup value in members table is updated with 1 for that logged in member who do not want popup to be see again.
I have searched for such thing but couldn't successful and I am getting no idea on it.
Try this.
yourphpfile.php
<?php
$resultsPop = mysql_query("select popup from members where mid=" . $mid);
$pop = mysql_result($resultsPop, 0, "popup");
if($pop == 0) {
$popupval = "<script type='text/javascript'>$(window).load(function(){ $('#MyPopUp').modal('show'); });</script>";
} else {
$popupval = "";
}
echo $popupval;
$ppop = $_POST['pops'];
$username = $_POST['mids'];
//Here you can update your database with 1 where username is mids.
//On success
$output = json_encode(array('type' => 'pop'));
die($output);
//Else
$output = json_encode(array('type' => 'error'));
die($output);
?>
<script>
$(document).ready(function(){
$("#closemodal").click(function(event){
event.preventDefault();
var popups = $('#popups').val();
var user_id = $('#mids').val();
$.ajax({
type: 'post',
url: yourphpfile.php,
dataType: 'json',
data: $('#yourform').serialize(),
success: function(data)
{
if(data.type == 'pop')
{
$('#MyPopUp').modal('hide');
}
}
});
});
});
</script>
<div class="modal fade" id="MyPopUp" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="">
<div class="modal-dialog modal-lg">
<div class="modal-content" style="">
<div class="modal-header">
<button type="button" class="close" id="closemodal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Welcome</h4>
</div>
<div class="modal-body" style="text-align: center;">
<span>
Welcome Popup
</span>
<form action="" id="yourform" method="POST" role="form">
<div class="checkbox">
<label><input type="checkbox" name="pops" id="popups" value="1">Pops</label>
</div>
<input type="hidden" id="mids" name="mids" value="<?php echo $_SESSION['mids']; ?>">
</form>
</div>
</div>
</div>
</div>