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.
Related
When I query from a page, I submit the form with jquery in sorgu.php. but how do i do it without making it infinite loop?
is there any other way ?
page link
header.php code search mod
<div class="modal fade" id="srcModal" tabindex="-1" role="dialog" aria-labelledby="contacthModal">
<div class="row searchArea modal-dialog modal-content" role="document">
<div class="container ">
<div class="row searchCloserow">
<div class="closeBtn" data-dismiss="modal"> <i class="fas fa-times" aria-hidden="true"></i> Kapat </div>
</div>
<div class="row searchForm d-flex justify-content-center">
<div class="col-md-2 searchText">
<span id="myModalLabel">Sipariş Sorgulama Ekranı</span>
<p>Sorgulamak istediğiniz siparişin kodunu giriniz!</p>
</div>
<div class="col-md-4 searchwell">
<input type="text" name="siparis_key" id="siparis_key" placeholder="Lütfen Sipariş Kodunu Giriniz">
</div>
<div class="col-md-2">
<a href="sorgu" id="siparis_Sorgula" type="submit" class="searchbtn anibut">
Sipariş Sorgula <i class="fas fa-question" aria-hidden="true"></i>
</a>
</div>
</div>
</div>
</div>
and header.php jquery code. here i am changing the href href="sorgu?key=asdasdasd"
$("#siparis_Sorgula").click(function(){
$data = $('#siparis_key').val();
$href = $("#siparis_Sorgula").attr('href');
$href = $href+"?key="+$data;
$("#siparis_Sorgula").attr("href", $href);
});
sorgu.php page code
<form action="" method="post" id="jsSorgula">
<div class="row">
<div class="col-lg-12 col-md-12">
<div class="form-group">
<input type="text" name="siparis_key" id="siparis_keyId" placeholder="<?php echo $dil['sipariskodu'] ?>" class="form-control" required>
</div>
</div>
<div class="col-lg-12 col-md-12">
<button type="submit" class="btn btn-primary btn-block"><?php echo $dil['sorgula'] ?></button>
</div>
</div>
</form>
sorgu.php jquery code
<script type="text/javascript">
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
$count = 0;
$(document).ready(function(){
if ($count==0) {
$keys = getUrlVars()["key"];
$('#siparis_keyId').val($keys);
$("#jsSorgula").submit();
$count++;
alert($count);
}
break;
});
The infinity loop could came from your $count = 0. Is set to 0 each time you make a request on that page. Instead of make the validation with count == 0, you should use a method like "on click" or "on change".
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>
EDIT! LATEST CODE UPDATED, NEW ERROR^
I am currently designing a website which has a feature for users to create an account.
I am encountering problems trying to get the user to be deleted first of all, then for the user to be deleted whilst logged in.
My Users controller looks like so:
public function destroy(Request $request)
{
$user = Auth::user();
Auth::logout();
if ($user->delete())
{
return Redirect::route('\home')->with('global', 'Your account has been deleted!');
}
}
My Modal bootstrap window that opens from a form looks like this;
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModalLabel">Are you sure?</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<form action="{{route('users.delete', ['user' => Auth::id()])}}" method="Post">
<span aria-hidden="true">×</span>
</button>
</div>
#csrf
#method('delete')
<div class="modal-body">
Are you sure you want to permanetly delete your account?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">No, cancel</button>
<button type="submit" class="btn btn-danger">Yes, delete my account</button>
</div>
</div>
</div>
</div>
The form the modal opens from:
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<img src="{{ asset('/uploads/avatars/' . $user->avatar ) }}" style="width:100px; height:100px; float:left;
margin-right:25px ">
<strong>Delete {{$user->name}}'s account?</strong></div>
<div class="card-body">
<form action="delete" method="POST" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="name">Account Email:</label>
<input type="text" name ="email" value="{{$user -> email}}" class="form-control" readonly>
<div class="form-group">
<div class="text-centre">
<p></p>
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#deleteModal">
Delete
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
And finally my route looks like this;
Route::post('/users/delete', 'Admin\UsersController#destroy')->name("delete-account");
Any ideas on how to firstly get this working and secondly implement it are welcome. thank you
Please tell us the error you are getting.
The first thing I recognize seeing your code is, that you return on the second line of your method. But after the return, you are still expecting code to run. Unfortunately, this will not work. Code after a return is ignored.
Try to split the methods:
Route::delete('/users/destroy', ['uses' =>'Admin\UsersController#destroy', 'as' => 'users.destroy']);
Route::get('/users/delete', ['uses' =>'Admin\UsersController#delete', 'as' => 'users.delete']);
Form method and request should be like this:
<form action="{{route(users.destroy)}}" method="delete">
Actual problem was the mismatch of route types. You have defined a route of GET type while the form is submitted to a route of DELETE type.
Please replace your code with this one and check:
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModalLabel">Are you sure?</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<form action="{{route('users.delete', ['user' => Auth::id()])}}" method="Post">
<span aria-hidden="true">×</span>
</button>
</div>
#csrf
#method('delete')
<div class="modal-body">
Are you sure you want to permanetly delete your account?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">No, cancel</button>
<button type="submit" class="btn btn-danger">Yes, delete my account</button>
</div>
</div>
</div>
</div>
And change your route to this
Route::delete('/users/{user}', 'Admin\UsersController#destroy')->name("users.delete");
Your controller lacks an IF-THEN statement
public function destroy(User $user)
{
if($user->id !== Auth::id()) return view('admin.users.delete')->with('user', Auth::user());
Auth::logout();
if ($user->delete())
{
return Redirect::route('\home')->with('global', 'Your account has been deleted!');
}
}
Please replace your code with this one and check:
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModalLabel">Are you sure?</h5>
<form action="{{route('delete-account'}}" method="Post">
#csrf
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</form>
</div>
<div class="modal-body">
Are you sure you want to permanetly delete your account?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">No, cancel</button>
<button type="submit" class="btn btn-danger">Yes, delete my account</button>
</div>
</div>
</div>
</div>
And change your route to this
Route::post('/users/delete', 'Admin\UsersController#destroy')->name("delete-account");
Your controller lacks an IF-THEN statement
public function destroy(Request $request)
{
$user = Auth::user();
Auth::logout();
if ($user->delete())
{
return Redirect::route('\home')->with('global', 'Your account has been deleted!');
}
}
How can i define transaction_id or initialize transaction_id in my laravel code i am stuck .
this the code on AdminContoller.php
public function provetransaction(Request $req, $transaction_id)
{
$transaction_id = transaction::find ($req->transaction_id);
$provetransaction = \DB::table('transaction')->where('transaction_id', $transaction_id)->first();
return view('provetransaction', compact('amount_paid', 'transaction_id'));
}
public function updateprove(Request $request, $transaction_id)
{
switch($request->get('approve'))
{
case 0:
Post::postpone($transaction_id);
break;
case 1:
Post::approve($transaction_id);
break;
case 2:
Post::reject($transaction_id);
break;
case 3:
Post::postpone($transaction_id);
break;
default:
break;
}
return redirect('view_all_transaction');
}
Below is my web.php where i have defined my routes.i have updated i have removed id i have used transaction_id instead.
web.php
Route::get('/view_all_transaction', 'AdminController#view_all_transaction')->name('admin');
Route::post('/view_all_transaction/{transaction_id}', 'AdminController#provetransaction')->name('admin');
Route::post('/view_all_transaction', 'AdminController#updateprove')->name('admin');
this my view_all_transaction.blade.php where the action is being passed
so where i am wrong.
<td>Moderate</td>
<div id="MyModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<!-- Modal Content: begins -->
<div class="modal-content">
<!-- Modal Header -->
<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="gridSystemModalLabel">Your Headings</h4>
</div>
<!-- Modal Body -->
<div class="modal-body">
<div class="body-message">
<h4>Approve </h4>
<div class="container">
<form method="post" action="action="{{action('AdminController#updateprove', $transaction_id)}}">
#csrf
<div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
<lable>Approval</lable>
<select name="approve">
<option value="0" #if($view_all_transaction->status==0)selected #endif>Pending</option>
<option value="1" #if($view_all_transaction->status==1)selected #endif>Approve</option>
<option value="2" #if($view_all_transaction->status==2)selected #endif>Reject</option>
<option value="3" #if($view_all_transaction->status==3)selected #endif>Postponed</option>
</select>
</div>
</div>
<!-- <div class="row">
<div class="col-md-4"></div>
<div class="form-group col-md-4">
<button type="submit" class="btn btn-success" style="margin-top:40px">Update</button>
</div>
</div> -->
</form>
</div>
</div>
</div>
<!-- Modal Footer -->
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button id="btnPrint" type="button" class="btn btn-default">Approve</button>
</div>
</div>
<!-- Modal Content: ends -->
The error is indicating that you used $id in your view code. In order to do so, you should compact a variable called "id" to your view.
Try
dd($req->all());
And make sure what key are in the request
I working on laravel project and I have many modals that I will using in my project so I decided to put modals code in another folder .
my view code is
<button type="button" class="btn btn-success" data-toggle="modal" data-
target="#AddUserMoodal"><i class="fa fa-edit" aria-hidden="true"></i>
when I pressing on this button I want to open AddModal which is in modals folder
this is code of /modals/addmodal.blade.php
<div id="addModal" class="modal fade" role="dialog">
<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"></h4>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form">
<div class="form-group">
<label class="control-label col-sm-2" for="title">Title:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="title_add" autofocus>
<small>Min: 2, Max: 32, only text</small>
<p class="errorTitle text-center alert alert-danger hidden"></p>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="content">Content:</label>
<div class="col-sm-10">
<textarea class="form-control" id="content_add" cols="40" rows="5"></textarea>
<small>Min: 2, Max: 128, only text</small>
<p class="errorContent text-center alert alert-danger hidden"></p>
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-success add" data-dismiss="modal">
<span id="" class='glyphicon glyphicon-check'></span> Add
</button>
<button type="button" class="btn btn-warning" data-dismiss="modal">
<span class='glyphicon glyphicon-remove'></span> Close
</button>
</div>
</div>
</div>
</div>
</div>
but I don't know how to do that .what is the route will be for it
suppose you are in home page : home.blade.php , use include
<body>
#include('models/addmodal')
</body>
If your modal is dynamic , and you want to pass data to modal :
<body>
#include('models/addmodal',['title'=>$title,'data'=>$data])
</body>
and in your addmodal
<div id="addModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
{{$title}}
</div>
</div>
</div>
</div>
Take a look at the #include directive.
Side note:
Just for reference, back in the old of Laravel you could solve it like this:
// routes.php (which is web.php now)
Route::get('view-component/{name}', function ($name) {
return view($name);
});
// In your view
{{ route('view-component/user-form') }}
You just need to include your blade file with
#include('path.to.your.blade')
You can have your HTML structure in parts and just include them as above.
Check Laravel documentation for blade: https://laravel.com/docs/5.6/blade#including-sub-views