Fetch data into modal popup using ajax in laravel - php

I am trying to fetch data from my database into a modal popup and make it dynamic... I'm also a bit confused on how to make the modal id unique for each data on the table... the "button" details pops up the modal.
This is my Table the buttons pops up a modal
This is my controller
public function show($upi)
{
$Patient = Patients::where('upi', '=', $upi)->first();
return Response::json($Patient);
}
This is my Route
Route::get('show-details/{upi}', [BssDashboardController::class, 'show'])->name('details.show');
This is my Ajax script
<script>
$(document).ready(function () {
$('body').on('click', '#show-details', function (){
var patientURL = $(this).data('url');
$.get('show-details/' + patientURL', function (d) {
$('#diagnosisModal').modal('show');
$('#date').text(data.date);
$('#age').text(data.age);
$('#gender').text(data.gender);
$('#plan').text(data.plan);
$('#authcode').text(data.authcode);
$('#findings').text(data.findings);
$('#compliants').text(data.compliants);
$('#investigation').text(data.investigation);
});
})
});
</script>
This is my Button
<a href="javascript:void(0)" id="show-details" data-url="{{ $item->upi }}">
<button class="btn-primary px-3 py-1">Details</button>
</a>
This is my modal
<div class="modal fade" id="diagnosisModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<div class="row">
<div class="col-8 pt-3">
<div class="d-flex flex-row"><label><b>Name:</b></label><div class="fw-600" id="name"></div></div>
<div class="d-flex flex-row"><label><b>DOB:</b></label><div class="fw-600" id="date">16-03-1990</div></div>
<div class="d-flex flex-row"><label><b>Age:</b></label><div class="fw-600" id="age">31 Years</div></div>
<div class="d-flex flex-row"><label><b>Sex:</b></label><div class="fw-600" id="gender">Female</div></div>
<div class="d-flex flex-row"><label><b>Plan:</b></label><div class="fw-600" id="plan">Equity</div></div>
<div class="d-flex flex-row"><label><b>Authcode:</b></label><div class="fw-600" id="authcode">-</div></div>
<div class="d-flex flex-row"><label><b>Findings:</b></label><div class="fw-600" id="findings">Pregnant</div></div>
<div class="d-flex flex-row"><label><b>Complaints:</b></label><div class="fw-600" id="compliants">ANC</div></div>
<div class="d-flex flex-row"><label><b>Investigations:</b></label><div class="fw-600" id="investigation">ANC</div></div>
</div>
<div class="col-4">
<div class="pt-4" style="width: 5rem;height: 4rem;"><img class="w-100" src="{{ asset('img/drake.jpg') }}" id="image" alt=""></div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-bs-dismiss="modal" style="background-color:#ff3030;border-radius:.7rem">Close</button>
</div>
</div>
</div>
</div>

Related

How to send the correct ID to the controller with ajax?

How do I add code to the input button so that the value of the Id is sent to the Controller?
I have eight images. Then I click on the id of 116, open this image.
When I click on the biggest photo, Open a bootstrap modal for me.
This action tag form is written to this ID 120, While I have selected ID 116.
http://localhost:8000/admin/products/4/galleries/120
#section('content')
<div class="col-md-10 p-5 pt-2">
<form method="post" action="{{ route('products.galleries.store', $product->id) }}" enctype="multipart/form-data" class="dropzone" id="dropzone">
#csrf
</form>
<hr>
<div class="col-md-6 mx-auto">
<div class="row">
<div class="mx-auto" id="showImage">
<img src="{{ asset("storage/{$gallery->image}") }}" class="img-fluid cursor-pointer" onclick="deleteImage('{{ $gallery->id }}', '{{ route('products.galleries.delete', [$product->id, $gallery->id]) }}')">
</div>
</div>
<div class="row">
#foreach($galleries as $gallery)
<div class="col-md-2">
<img src="{{ asset("storage/{$gallery->image}") }}" class="img-fluid cursor-pointer" onclick="showImage('{{ asset("storage/{$gallery->image}") }}', '{{ $gallery->id }}')"><br><div class="text-center text-danger">{{ $gallery->id }}</div>
</div>
#endforeach
</div>
</div>
</div>
<div class="modal fade" id="delete" data-backdrop="static" data-keyboard="false" tabindex="-1" aria-labelledby="delete" aria-hidden="true">
<div class="modal-dialog modal-sm">
<form action="{{ route('products.galleries.delete', [$product->id, $gallery->id]) }}" method="post">
#csrf
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">This action is not reversible.</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Are you sure you want to delete the image?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger">Delete</button>
</div>
</div>
</form>
</div>
</div>
#endsection
#push('script')
<script src="{{ asset('themes/js/dropzone.min.js') }}"></script>
<script>
function showImage(url, id) {
let route = "'"+'{{ route('products.galleries.delete', [$product->id, $gallery->id]) }}'+"'";
let image = '<img src='+url+' onclick="deleteImage('+id+','+route+')" class="img-fluid cursor-pointer" />';
$('#showImage').html(image);
}
function deleteImage(id, url) {
$('#delete').modal(id, url);
}
</script>
#endpush
web.php
Route::post('products/{product}/galleries/{gallery}', 'GalleryController#delete')->name('products.galleries.delete');

Livewire submit form creates duplicate records in database

I have a form in modal and I'm using Livewire, what's really happens is when I submit the form everything works correctly but it just creates duplicate records!
The first time creates 2 duplicates, the second time creates 3 and more and more.
I don't know how to fix this!
I'd be appreciated for your help.
this is my livewire view file :
<div class="modal fade" id="createProfessionModal" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="staticBackdrop" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<!--begin::Container-->
<div class="card card-custom card-shadowless rounded-top-0">
<!--begin::Body-->
<div class="card-body p-0">
<div class="row justify-content-center py-8">
<!--begin::Wizard Form-->
<form class="form fv-plugins-bootstrap fv-plugins-framework" id="createProfessionForm" method="post" wire:submit.prevent="store">
<div class="justify-content-center">
<!--begin::Wizard-->
<div class="my-5 step">
<h5 class="text-dark font-weight-bold mb-10">ایحاد حرفه جدید</h5>
<!--begin::Group-->
<div class="form-group row fv-plugins-icon-container">
<label class="col-form-label" for="profession_title">عنوان حرفه</label>
<div class="col-lg-9 col-xl-9">
<input id="profession_title" class="form-control form-control-solid form-control-lg" wire:model.defer="title" name="title" type="text">
<div class="fv-plugins-message-container"></div>
</div>
</div>
<!--end::Group-->
<button type="submit" class="btn btn-primary font-weight-bolder px-9 py-4" >ایجاد</button>
<button type="button" data-dismiss="modal" class="btn btn-outline-info font-weight-bolder px-9 py-4" >بستن</button>
</div>
<!--end::Wizard-->
</div>
</form>
</div>
</div>
</div>
<!--end::Wizard Form-->
</div>
</div>
</div>
script:
document.addEventListener('DOMContentLoaded', function () {
$('#createProfessionForm').on('submit', function (e) {
e.preventDefault();
let professionValue = $('#profession_title').val();
#this.set('title', professionValue, true);
#this.store();
})
})
this is my component
public string $title;
public function store()
{
try {
$profession = new Profession();
$profession->name = $this->title;
$profession->save();
session()->flash('success', 'عملیات با موفقیت انجام شد.');
} catch (\Exception $exception) {
session()->flash('error', __($exception->getMessage()));
}
}
If you are using livewire, you don't need to bind the event in Javascript like you're doing here.
Because if you do that, the request is firing twice. You can check for the same in the network as well.

I want the modal to open with the correct ID i.e. in the foreach

I have a problem: I used a foreach to list a database collection with a button to open a modal in each one. The button works correctly, but the ID that passes into the modal does not. If the modal code is inside the foreach, in all options the first id always appears, if it is outside the foreach the last ID always appears.
<div class="row">
#if ($trainingphases == null)
<p>null</p>
#else
<div class="col-md-6">
<div class="card mt-1">
<div class="card-body">
<!-- right control icon -->
#foreach ($trainingphases as $trainingphase)
<div class="accordion" id="accordionRightIcon">
<div class="card ">
<div class="card-header header-elements-inline">
<h6 class="card-title ul-collapse__icon--size ul-collapse__right-icon mb-0">
<a data-toggle="collapse" class="text-default collapsed" href="#accordion-item-icon-right-{!!$trainingphase->id_trainingphases!!}"
aria-expanded="false">{!! $trainingphase->title !!}</a>
</h6>
</div>
<div id="accordion-item-icon-right-{!!$trainingphase->id_trainingphases!!}" class="collapse" data-parent="#accordionRightIcon" style="">
<div class="card-body">
<textarea class="ckeditor form-control" placeholder="teste" name="description">{{ $trainingphase->description }}</textarea>
</div>
</div>
<div class="col-md-2">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg">editar {!! $trainingphase->title !!}</button>
</div>
</div>
</div>
#endforeach
<!-- /right control icon -->
</div>
</div>
</div>
#endif
</div>
<div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<form method="POST" action="{{ route('create.trainingphase', array('id_trainings'=>$training->id_trainings)) }}">
#csrf
<h5 class="modal-title" id="exampleModalCenterTitle"><input type="text" class="form-control form-control-rounded" id="title" placeholder="{!! $trainingphase->title !!}" name="title"></h5>
</div>
<div class="modal-body">
<label for="descriptiom">Descrição</label>
<textarea class="ckeditor form-control" placeholder="teste" name="description">{{ $trainingphase->description }}</textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
Explaination
This is the expected behaviour of a loop within PHP.
So your code will evaluate from top to bottom and within your #foreach loop you are essentially re-assigning the value of $trainingphase within the scope of the script.
Once the loop is finished, the value of $trainingphase is not unset so it will be whatever the last value was in the object you had just looped through.
Solution
Keep the modal code outside of the loop and try using data properties on the buttons you are using to open the modal to pass values into the modal itself.
Documentaion on this can be found here:
https://getbootstrap.com/docs/4.0/components/modal/#varying-modal-content

Dynamically pass data to a Modal from a dynamically created set of images using details from a MySQL database

I have created multiple objects using data retrieved from a MySQL database. Each object contains a clickable image and 2 headers. I can click an image which is connected to a modal but it only gives back 1 modal with the information from the very last object no matter which image is clicked.
Please help.
The following part creates the objects and works.
<div class="row kprod">
<?php
$get_kits = "select * from kitchens ORDER BY k_brand";
$run_kits = mysqli_query($con, $get_kits);
while($row_slides=mysqli_fetch_array($run_kits)){
$kit_brand = $row_slides['k_brand'];
$kit_name = $row_slides['k_name'];
$kit_img = $row_slides['k_img'];
$kit_desc = $row_slides['k_description'];
$kit_id = $row_slides['k_id'];
echo"
<div class='col-12 col-xs-12 col-md-6 col-lg-4 kpindi'>
<div class='row'>
<div class='col imgcol'>
<img class='img-fluid' src='assets/img/$kit_img' href='#portfolio-modal-1' data-toggle='modal'>
</div>
</div>
<div class='row'>
<div class='col'>
<div>
<h5 class='kprodtext' style='font-family:Adamina, serif;'>$kit_brand</h5>
<h6 class='kprodtext' style='font-family:Adamina, serif;'><em>$kit_name</em></h6>
</div>
</div>
</div>
</div>";}
?>
</div>
The next part is the modal I want to pass the data too.
<div id="portfolio-modal-1" class="modal text-center" role="dialog" tabindex="-1">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">x</span></button>
</div>
<div class="modal-body">
<div class="container text-center">
<div class="row">
<div class="col-lg-8 mx-auto">
<h2 class="kprodtext" style="font-family:Adamina, serif;">
Brand</h2>
<hr class="star-dark mb-5">
<img src="assets/img/image.jpg" class="img-fluid mb-5">
<h6 class="kprodtext mb-5" style="font-family:Adamina, serif;">
<i>Name</i>
</h6>
<p class="kprodtext" style="font-family:Adamina, serif;">
Desc
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
You need to assign different ids to your modals, otherwise only one will be opened.

Creating a custom Woocommerce product modal on click

I have created a basic HTML/jQuery structure for a modal that will pop up when clicked on. I'm in the process of integrating Woocommerce into my site and am unsure as to how I can do the following:
Have modal load respective product info
Integrate into a Woocommerce loop
Here is my HTML for the skeleton of what I'm trying to achieve:
<div class="col-md-4"><img src="<?php bloginfo('stylesheet_directory'); ?>/media/temp_product_images/Alto-183x300.jpg" alt="Product Img" class="product-img" data-toggle="modal" data-target="#myModal">
<p class="product-img-title text-center">Alto</p>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<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 text-center" id="myModalLabel">Alto</h4> </div>
<div class="modal-body" id="modal-body-one">
<div class="row">
<div class="col-sm-4"> <img src="<?php bloginfo('stylesheet_directory'); ?>/media/temp_product_images/Alto-183x300.jpg" alt="Modal Product Img" id="product-img"> </div>
<div class="col-sm-7">
<h5>Colours</h5>
<div class="scroll-container">
<div class="row">
<div class="col-xs-4">
<div class="selected-product-color"><img src="<?php bloginfo('stylesheet_directory'); ?>/media/temp_product_images/MACASSA.jpg" alt="Product Colour">
<p>Macassa</p>
</div>
</div>
</div>
<!-- End of Test Case -->
</div>
</div>
<div class="col-sm-1">
<button type="button" class="btn flip"><i class="fa fa-arrow-right" aria-hidden="true"></i></button>
</div>
</div>
</div>
<!-- Prices -->
<div class="modal-body" id="modal-body-two">
<div class="row">
<div class="col-sm-11">
<div class="row">
<h5 id="modal-prices">Download Price List</h5></div>
<div class="row">
<?php $my_query = new WP_Query('p=47');
while($my_query->have_posts()){
$my_query->the_post();
the_content();
} ?>
</div>
</div>
<div class="col-sm-1">
<button type="button" class="btn flip"><i class="fa fa-arrow-right" aria-hidden="true"></i></button>
</div>
</div>
</div>
<div class="modal-footer"> Get Quote </div>
</div>
</div>
</div>
<!-- End of Modal -->
</div>
And here is my jQuery:
//Modal Change Content
$('#modal-body-two').hide();
$('.flip').on('click', function () {
$('#modal-body-one, #modal-body-two').toggle()
});
Is there a way that I can display the product info on click within a modal using Woocommerce? Thanks

Categories