Pass data form controller to ajax and modal laravel 8 - php

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>

Related

how to add "loading" message into bootstrap modal until data fetch from database

I am using bootstrap modal to show to data which is fetched from the database. I have this code for load remote PHP data into bootstrap modal. it is work. but i need to add a loading message into the modal until data fetch from database. I have this code for load remote PHP data into bootstrap modal box
index.html
<script>
$(document).ready(function(){
$('#myModal').on('show.bs.modal', function (e) {
var rowid = $(e.relatedTarget).data('id');
$.ajax({
type : 'post',
url : 'fetch_subjects.php', //Here you will fetch records
data : 'rowid='+ rowid, //Pass $id
success : function(data){
$('.fetched-data').html(data);//Show fetched data from database
}
});
});
});
</script>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Subjects</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="fetched-data"> </div> <!--fetched dates here -->
</div>
<div class="modal-footer">
<button type="butZton" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
fetch_subjects.php
<?php
require 'db_connection.php';
if($_POST['rowid']) {
$id = $_POST['rowid'];
$sql = "SELECT * FROM subject where year=$id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div class='list-group'>
<b>
<a href='notes?id=". $row['id'] ."' class='list-group-item list-group-item-action' target='_blank'>
" . $row["name"]. "
</a>
</b>
</div>
</button>";
}
}
else {
echo "no details.";
}
}
?>
I would do that this way:
$.ajax({
type : 'post',
url : 'fetch_subjects.php', //Here you will fetch records
data : 'rowid='+ rowid, //Pass $id
success : function(data){
$('.fetched-data').html(data);//Show fetched data from database
},
beforeSend: function() {
$('#loader').show(); // Assuming that you have some loader defined
},
complete: function(){
$('#loader').hide(); //Hide this loader
},
});
<script>
$(document).ready(function(){
$('#myModal').on('show.bs.modal', function (e) {
var rowid = $(e.relatedTarget).data('id');
$.ajax({
type : 'post',
url : 'fetch_subjects.php', //Here you will fetch records
data : 'rowid='+ rowid, //Pass $id
beforeSend: function() {
$('.loader').show(); // Assuming that you have some loader defined
},
success : function(data){
$('.fetched-data').html(data);//Show fetched data from database
$('.loader').hide();
},
});
});
});
</script>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Subjects</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<!-- loader -->
<div class="loader">
loading.............
</div>
<div class="fetched-data"> </div> <!--fetched dates here -->
</div>
<div class="modal-footer">
<button type="butZton" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

My popup windows wont show even using script

i have try everything but still doesn't work. My popup windows only up until header and button but it wont show the body where all the data is in.
$(document).on('click','.delete_data',function(){
var delete_id = $(this).attr('id') ;
$.ajax ({
URL : "showbekalan.php",
type : "post",
data = {delete_id:delete_id},
success: function(data){
$("#deleteInfo").html(data);
$("#delete").modal('show');
}
});
});
<!-- Modal content-->
<td class="column100 column6" data-column="column6">
<a href="#delete" type="button" class="btn btn-danger delete_data" id="<?php echo $id ;?>" data-toggle="modal">
<font size="3">Padam</font>
</a>
</td>
<div class="modal fade" id="delete" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">
<font size="6px"><b>Padam</b></font>
</h4>
</div>
<div class="modal-body" id="deleteInfo">
<form class="contact100-form validate-form" action="deletebekalan.php?bid=<?php echo $id;?>" method="post" id="deleteForm">
<div class="container-contact100-form-btn">
<button class="contact100-form-btn" id="deleteButt">
<span>
Padam
<i class="zmdi zmdi-arrow-right m-l-8"></i>
</span>
</button>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Tutup</button>
</div>
</div>
</div>
</div>
Try to change this:
$(document).on('click','.delete_data',function(){
var delete_id = $(this).attr('id') ;
$.ajax ({
URL : "showbekalan.php",
type : "post",
data = {delete_id:delete_id},
success: function(data){
$("#deleteInfo").html(data);
$("#delete").modal('show');
}
});
});
to this:
$(document).on('click','.delete_data',function(){
var delete_id = $(this).attr('id') ;
$.ajax ({
URL : "showbekalan.php",
type : "post",
data = {delete_id:delete_id},
success: function(data){
$("#delete").modal('show');
$("#delete #deleteInfo").html(data); //Assuming that modal window has class modal
}
});
});

Ajax calls problems

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) {...});

Printing A Data Inside A Modal

When I hit the View button, that modal will pop-up. The modal contains details to be printed. Can you please help what should I do with it? Thank you in advance.
This will be the pop-up modal when I hit the view button.
The code below was used for the view button:
<div id="dataModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Work Request</h4>
</div>
<div class="modal-body" id="request_detail">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" onclick="window.print();">Print</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
And the script for the View Button:
$(document).on('click', '.view_data', function(){
var request_id = $(this).attr("id");
if(request_id != '')
{
$.ajax({
url:"select.php",
method:"POST",
data:{request_id:request_id},
success:function(data){
$('#request_detail').html(data);
$('#dataModal').modal('show');
}
});
}
});
This one is for the Print button:
<script>
$().ready(function () {
$('.modal.printable').on('shown.bs.modal', function () {
$('.modal-dialog', this).addClass('focused');
$('body').addClass('modalprinter');
if ($(this).hasClass('autoprint')) {
window.print();
}
}).on('hidden.bs.modal', function () {
$('.modal-dialog', this).removeClass('focused');
$('body').removeClass('modalprinter');
});
});
When I hit the Print the button, it's working but, (just look at the photo): This is the result of clicking the Print button.

pass the value of mysql to bootstrap modal

i want to select from a list of data and display the result of the selected data in a modal popup. i just can't figure out how to pass the id to the popup so i can display the result. HELP!!
<td> <button class="btn btn-primary " data-toggle="modal" data-id="<?php $id= $row['id'] ?>" data-target="#myModal" <?php echo"id=$row[id]'";?> >
View Details
</button>
</td>
the modal popup
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<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">Requisition Details</h4>
</div>
<div class="modal-body">
<?php
$id = $_GET['id'];
include('mysql_connect.php');
$query11 = mysql_query ("select * from p_requisition where id = '$id' ") or die(mysql_error());
$rows= mysql_fetch_array($query11);
echo $rows['details'];
?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
Firstly, add class to button may be get-data,
then in jquery,
$(document).ready(function() {
$(document).on("click",".get-data"function(){
var val = $(this).attr("data-id");
$.ajax({
url: "path to ajax file",
type: "POST",
dataType: "HTML",
async: false,
success: function(data) {
$('.modal-body').html(data);
}
});
});
});
in ajax file,write query to fetch data from mysql, manipulate the data and display in popup.
ajax.php
<?php
$id = $_POST['id'];
include('mysql_connect.php');
$query11 = mysql_query ("select * from p_requisition where id = '$id' ") or die(mysql_error());
$rows= mysql_fetch_array($query11);
echo $rows['details'];// here you need to manipulate the database data with html and pass it on to popup.
?>
use like
<td>
<button class="btn btn-primary" onclick='openModel(<?php echo"id=$row[id]";?>') >View Details</button>
</td>
open your model like
<script>
function openModel(modelId,rowId){
$('#'+modelId).model('open');
//Ajax call to get data for speciefic id
}
</script>
I think you should use ajax call, when open modal like this:
Your modal:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<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">Requisition Details</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
Your button:
<button class="btn btn-primary " data-toggle="modal" data-id="<?php echo $row['id'] ?>" data-target="#myModal" id="myButton">
View Details
</button>
When click the button, show the modal:
$('#myButton').click(function() {
$('#myModal').modal('show')
});
If the modal shown, you pass the data with ajax
$('#myModal').on('shown.bs.modal', function(e) {
$.ajax({
method: "POST",
url: "getData.php",
data: {
dataId: $('#myModal').attr('data-id')
},
success: function(data) {
data = jQuery.parseJSON(data);
$('.modal-body', '#myModal').html(data);
}
});
})
Your getData.php like this
<?php
//dataId comes from ajax (POST)
$id = filter_input(INPUT_POST, 'dataId');
include('mysql_connect.php');
$query11 = mysql_query("select * from p_requisition where id = '$id' ") or die(mysql_error());
$rows = mysql_fetch_array($query11);
//you should use json_encode, and you can parse when get back data in ajax
echo json_encode($rows['details']);

Categories