Laravel user edit - php

I have user details from DB and print using blade loop with button for edit. now I want, if I click button I need user details for the button which is clicked for user.
i tried with hidden input filed <input type="hidden" value="{{$user->email}}" name="email">
in my controller I just print the value from form submit. but I am getting only last user.
See below, I hope you will understand..
<div class="content-wrapper">
<div class="flex-center position-ref full-height">
<form action="{!!url('/delete')!!}" method="post"> {!!csrf_field()!!}
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Role</th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td> <input type="hidden" value="{{$user->email}}" name="email">
<td>{{$user->role}}</td>
<td>
<div class="btn-group">
<button type="submit" class="btn btn-primary btn-sm" name="edit" >Edit</button>
<button type="submit" class="btn btn-primary btn-sm" name="delete" >Delete</button>
<button type="submit" class="btn btn-primary btn-sm" name="make" >Make Admin</button>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</form>
</div>
</div>

Assuming that you want to go to edit page with a user's details when you click edit button.
Edit

I think that's because you are using same name "email" for all user's hidden field. It is getting overridden, since form is same .
Instead, add the form tag inside your loop and remove the one before table.
#foreach($users as $user)
<form action="{!!url('/delete')!!}" method="post"> {!!csrf_field()!!}
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td> <input type="hidden" value="{{$user->email}}" name="email">
<td>{{$user->role}}</td>
<td>
<div class="btn-group">
<button type="submit" class="btn btn-primary btn-sm" name="edit" >Edit</button>
<button type="submit" class="btn btn-primary btn-sm" name="delete" >Delete</button>
<button type="submit" class="btn btn-primary btn-sm" name="make" >Make Admin</button>
</div>
</td>
</tr>
</form>
#endforeach
Or, you can put a single hidden email field outside your loop and assign it's value using JS before submitting.
Step 1: Add data attribute (data-email) to your buttons to get email. Also add a common class name to capture click event. (See JS part)
<button type="button" class="test-btn btn btn-primary btn-sm" name="edit" data-email="{{$user->email}}" >Edit</button>
<button type="button" class="test-btn btn btn-primary btn-sm" name="delete" data-email="{{$user->email}}" >Delete</button>
<button type="button" class="test-btn btn btn-primary btn-sm" name="make" data-email="{{$user->email}}" >Make Admin</button>
Step 2: Move email hidden field outside the loop with value as blank.
<form action="{!!url('/delete')!!}" method="post" id="test-form"> {!!csrf_field()!!}
<input type="hidden" value="" name="email" id="email">
Step 3: Add JS to capture click event on the buttons and assign the value of the clicked button's email to the email text field.
$(".test-btn ").on("click",function(e){
//assign the email value from button to variable
email = $(this).attr(data-email);
//assign the email value to email hidden field
$("#email").value(email);
//submit the form
$("#test-form").submit();
});

Related

$request->has() doesn't work with Button type and using jQuery in Laravel controller

I have question on Laravel with jQuery.
Right now, I have form with three (3) different buttons (update,delete,email). Each button have different task in controller.
Normally I use button type="submit" to submit data from blade into controller.
Blade
<form id='form-info' class="form-info" action="{{url('/form/sendData')}}" method="post">
#csrf
<input type="text" id="info" class="info" name="name" value="test"/>
<button type="submit" class="btn btn-xs btn-warning" name="update" id="update">Update</button>
<button type="submit" class="btn btn-xs btn-danger" name="delete" id="delete">Delete</button>
<button type="submit" class="btn btn-xs btn-info" name="email" id="email">Email</button>
</form>
And in controller, I just use '$request->has()' to differentiate three request.
if ($request->has('update'))
{
echo 'update';
}
else if($request->has('delete'))
{
echo 'delete';
}
else if($request->has('email'))
{
echo 'email';
}
But my current goal is to show loading icon before data successfully submitted.
The reason why I want to show loading page is because system need to send an email to multiple user and it might take some time to load before it finished the task. To prevent user to click the same button multiple times.
I use jQuery click function and I change button type from 'submit' to 'button'.
But the problem is when the form submitted to controller. It doesn't recognize '$request->has()' in my controller.
Here's my html form and jQuery and my controller
blade.php
<form id='form-info' class="form-info" action="{{url('/form/sendData')}}" method="post">
#csrf
<input type="text" id="info" class="info" name="name" value="test"/>
<button type="button" class="btn btn-xs btn-warning" name="update" id="update">Update</button>
<button type="button" class="btn btn-xs btn-danger" name="delete" id="delete">Delete</button>
<button type="button" class="btn btn-xs btn-info" name="email" id="email">Email</button>
</form>
jQuery
$(document).ready(function(){
$('#update').click(function(){
$(".loader").show();
$("#form-info").submit();
$(".loader").hide();
});
$('#delete').click(function(){
$(".loader").show();
$("#form-info").submit();
$(".loader").hide();
});
$('#email').click(function(){
$(".loader").show();
$("#form-info").submit();
$(".loader").hide();
});
});
Controller.php
if ($request->has('update'))
{
echo 'update';
}
else if($request->has('delete'))
{
echo 'delete';
}
else if($request->has('email'))
{
echo 'email';
}
How to solve this problem ? Thanks in advance.

Increase the Number of item if its out of stock

I'm just gonna ask what's the best way to do Increase the number of items if it's out of stock or below 100.
Like Medicine is 95 and you input 5 then click add button then the result will be 100.
<td>{{ $medicine->medicine_qty }}
<input style="width: 50px;" class="box" type="number" id="qty">
<button type="submit" class="btn btn-secondary" data-dismiss="modal" id="addMed">Add</button>
<button type="submit" class="btn btn-secondary" data-dismiss="modal" id="minusMed">Minus</button>
</td>
In front end you can add name tag in your buttons and in backend have a code something like this:
<td>{{ $medicine->medicine_qty }}
<input style="width: 50px;" class="box" type="number" id="qty" name="qty">
<button type="submit" class="btn btn-secondary" data-dismiss="modal" id="addMed" name="addMed">Add</button>
<button type="submit" class="btn btn-secondary" data-dismiss="modal" id="minusMed" name="minusMed">Minus</button>
</td>
$qty = $request->get('qty');
if($request->get('addMed')){
// add value to your medicine
$yourModelValue += $qty;
} elseif($request->get(''){
// remove value from medicine
$yourModelValue -= $qty;
}
You will need to submit a request if you want to update the data. If you want a button press to trigger it without leaving or refreshing the page, then you'll probably want to make an AJAX request and have it update the value in the database, then return the new value you will want to reflect on the page.

Pass Data from input form to ajax in Laravel

I Have a List of projects and three type of payments for any project I Want to give all type of payments and send payment id to ajax But I give just id=1 for all request my Code is here:
#foreach($project->payments as $payment)
#if($payment->type==1)
#if(empty($payment->code))
<input type="hidden"id="payment_id"value="{{$payment->id}}">
<input type="text" class="form-control" id="paymentCode" name="paymentC">
{{csrf_field()}}
<button id="AddCode" type=submit class="btn btn-info generate-label AddCode">enter</button>
#else
{{$payment->code}}
#endif
#elseif($payment->type==2)
#if(empty($payment->code))
<input type="hidden" id="payment_id" value="{{$payment->id}}">
<input type="text" class="form-control" name="paymentC" id="paymentCode">
{{csrf_field()}}
<button id="AddCode" type="submit" class="btn btn-info generate-label AddCode">enter</button>
#else
{{$payment->code}}
#endif
#endif
#endforeach
Javascript Code:
$('.AddCode').click (function (event) {
var payment_id = $('#payment_id').val();
var paymentCode = $('#paymentCode').val();
console.log(payment_id);
});
How I Can fix my problem that When click in button of payment print this id?
You're not targetting an unique id with var payment_id = $('#payment_id').val();
Change this line of html
<button id="AddCode" type="submit" class="btn btn-info generate-label AddCode">enter</button>
Into
<button value="{{$payment->id}}" type="submit" class="btn btn-info generate-label AddCode">enter</button>
JQuery
$('.AddCode').on("click", function(event){
var payment_id = $(this).val(); // payment id
});
Try this
#foreach($project->payments as $payment)
#if($payment->type==1)
#if(empty($payment->code))
<div>
<input type="hidden" id="payment_id" value="{{$payment->id}}">
<input type="text" class="form-control" id="paymentCode" name="paymentC">
{{csrf_field()}}
<button id="AddCode" class="btn btn-info generate-label AddCode">enter</button>
</div>
#else
{{$payment->code}}
#endif
#elseif($payment->type==2)
#if(empty($payment->code))
<div>
<input type="hidden" id="payment_id" value="{{$payment->id}}">
<input type="text" class="form-control" name="paymentC" id="paymentCode">
{{csrf_field()}}
<button id="AddCode" class="btn btn-info generate-label AddCode">enter</button>
</div>
#else
{{$payment->code}}
#endif
#endif
#endforeach
Javascript Code:
$('.AddCode').click (function (event) {
var payment_id = $(this).closest('div').find("#payment_id").val();
var paymentCode = $(this).closest('div').find("#paymentCode").val();
console.log(payment_id);
});
ID should be unique for individual HTML entity.
Code Update:
{{csrf_field()}}
#foreach($project->payments as $payment)
#if($payment->type==1)
#if(empty($payment->code))
<input type="hidden"id="payment-id-1"value="{{$payment->id}}">
<input type="text" class="form-control" id="payment-code-1" name="payment_code">
<button type=submit class="btn btn-info generate-label add-code" data-payment=1>enter</button>
#else
{{$payment->code}}
#endif
#elseif($payment->type==2)
#if(empty($payment->code))
<input type="hidden" id="payment_id_2" value="{{$payment->id}}">
<input type="text" class="form-control" name="payment_code" id="paymentCode">
<button type="submit" class="btn btn-info generate-label add-code" data-payment=1>enter</button>
#else
{{$payment->code}}
#endif
#endif
#endforeach
Javascript Code:
$('.add-code').on("click", function(event){
var payment_type = $(this).data("payment");
var payment_id = $('#payment-id-'+payment_type).val();
var paymentCode = $('#payment-code-'+payment_type).val();
});
Try this out. Not tested.

hidden field how to remove last part of url?

I want to remove GET parameters from urls:
http://localhost/fadt/admin/edit_country.php?id=7
I want my URLs to become:
http://localhost/fadt/admin/edit_country
here is how links are currently generated:
<button onClick="location.href='edit_country?id=<?php echo $row['COUNTRY_ID']; ?>'" class="btn btn-primary btn-xs" title="Edit"><i class="fa fa-pencil"></i></button>
Instead of a button use an HTML form with the POST method.
Something like:
<form action="edit_country.php" method="post">
<input type="hidden" value="<?php echo $row['COUNTRY_ID'];?>" name="id" />
<input type="submit" class="btn btn-primary btn-xs fa fa-pencil" title="Edit"/>
</form>
in place of
<button onClick="location.href='edit_country?id=<?php echo $row['COUNTRY_ID']; ?>'" class="btn btn-primary btn-xs" title="Edit"><i class="fa fa-pencil"></i></button>
Then on edit_country.php use $_POST instead of $_GET.
This id can be manipulated just as easily as your button value so make sure you are authenticating executing user has permissions to perform action.
Update, using button:
<form method="post">
<input type="hidden" value="<?php echo $row['COUNTRY_ID'];?>" name="id" />
<button type="submit" class="btn btn-primary btn-xs" title="Edit"><i class="fa fa-pencil"></i></button>
</form>
Not clear the question but i think u need this
<?php
$a = explode('?','http://localhost/fadt/admin/edit_country.php?id=7');
echo rtrim($a[0], '.php')
?>

Submitting to database from modal view

I have a file, let`s name it dashboard.php, in this file i load a modal window and trough this window i want to submit a form and the form action is also written in this dashboard.php file. is this possible? i am acutally trying it this way, but it s not working:
dashboard:php
<?php $modal_gerade_geschlossen = $_POST['abgespeichert'];
if ($modal_gerade_geschlossen == 1){
$neue_addy = $_POST['neue_addy'];
$benach_ja_nein = $_POST['benach_ja_nein'];
mysql_query("INSERT INTO mitglieder (email,benachrichtigung)VALUES('$neue_addy','$benach_ja_nein')");
}
?>
<div class="modal-body" style="height:194px">
<div>
<form name="losgehts" method="post" action="../dashboard.php">
<div style="float:left">
<input name="neue_addy" style="width:270px" type="text"/>
</div>
</div>
<div>
<select name="benach_ja_nein" >
<option>ja</option>
<option>nein</option>
</select>
</div>
<input type="hidden" name="abgespeichert" value="1"/>
<button type="button" class="btn btn-default" data-dismiss="modal">Schließen</button>
<input type="submit" name="submit" class="btn btn-primary" value="Speichern"/>
</form>
</div>
</div>

Categories