Increase the Number of item if its out of stock - php

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.

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.

Laravel user edit

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();
});

How to add (or subtract) a value by pressing the submit button PHP

I have the following code=>
<button class="btn btn-lg btn-warning text-center but-width" id="first" name="geri" type="submit" value="<?php ?>">BACK</button>
<button class="btn btn-lg btn-warning text-center but-width" id="second" name="ileri" type="submit" value="<?php ?>">FORWARD</button>
These buttons submit the form to the same page. At the top of the page I have variable $t which is initialized at value '0' and is followed by the codes=>
$t=0;
if( $this->input->post('ileri') ) { $t=$t+1;} // Back button was pressed;
if( $this->input->post('geri') ) { $t=$t-1;} // Forward button was pressed;
Now as I press forward I want to increase $t and when I press back I want to decrease it. But because of the declaration at the top ($t=0;) it goes as far as '1' with 'forward' and as back as '0' with 'back' buttons. Can anyone think of a way of getting over this problem.
Thank you...
Add a hidden input, and store $t there. This gets posted upon submit:
<?php
$t = $this->input->post('t');
if (!$t) $t = 0;
if( $this->input->post('ileri') ) { $t=$t+1;} // Back button was pressed;
if( $this->input->post('geri') ) { $t=$t-1;} // Forward button was pressed;
?>
<input name="t" type="hidden" value="<?=$t ?>">
<button class="btn btn-lg btn-warning text-center but-width" id="first" name="geri" type="submit" value="submit">BACK</button>
<button class="btn btn-lg btn-warning text-center but-width" id="second" name="ileri" type="submit" value="submit">FORWARD</button>
You could use url parameter. Get instead of post. so that it stores the current value of variable $t i.e.
if (isset($_GET['t']))
{
$t = $_GET['t'];
}
else
{
$t = 0;
}
Something like that could be of some help
If you want to know what button makes the submit you need to change the buttons
<button class="btn btn-lg btn-warning text-center but-width" id="first" name="geri" type="submit" value="<?php ?>">BACK</button>
<button class="btn btn-lg btn-warning text-center but-width" id="second" name="ileri" type="submit" value="<?php ?>">FORWARD</button>
to
<button class="btn btn-lg btn-warning text-center but-width" id="first" name="s_button" type="submit" value="geri">BACK</button>
<button class="btn btn-lg btn-warning text-center but-width" id="second" name="s_button" type="submit" value="ileri">FORWARD</button>
in your controller
$button_s = $this->input->post('s_button');
if( isset($button_s)){
switch($button_s){
case 'geri':
//do somthing
break;
case 'ileri':
//do somthing
break;
}
}

Bootstrap button, how to click multiple times

I have a counter which adds number of rows to a table based on what the counter is. I have one button that increase the counter every time its clicked, but it only works one time.
php:
if (isset($_REQUEST['Addmore'])) {
$NoOfUsers++;
<form action="addstudent.php">
<th>
<button type="submit" class="btn btn-default btn-sm spoiler-trigger pull-right" aria-label="Left Align" name="Addmore" title="Add Row">
<span class="glyphicon glyphicon-thumbs-up" aria-hidden="true"> </span>
</button>
</th>
</form>
php
<?php
session_start();
if (isset($_REQUEST['Addmore'])) {
?><button class="btn btn-default"><?php
echo $_SESSION['NoOfUsers'] ? ++$_SESSION['NoOfUsers'] : $_SESSION['NoOfUsers']=1;
?></button><?php
}
or JavaScript
<button id="counter" class="btn btn-default">0</button>
<script>
$('#counter').on('click',function(){$( this ).html(1 * $( this ).html() + 1)});
</script>

how to know which buttons submits the form php

i have two buttons of type submit in my view:
<button type="submit" class="btn btn-default btn-xs" name="Excel">
<span class="glyphicon glyphicon-download-alt"></span> Download as Excel
</button>
<button type="submit" class="btn btn-default btn-xs" name="Resume">
<span class="glyphicon glyphicon-download-alt"></span> Download Resume
</button>
and controller:
if ($this->input->post('Resume')) {
echo "Resume Clicked";
}
if ($this->input->post('Excel')) {
echo "Excel Clicked";
}
i would like to know that which button has submitted the form for that i have written the above code..but i am not getting any echo after clicking any of the button..
i don't want to use here:
<input type='submit' name='excel' value='Excel'/>
<input type='submit' name='excel' value='Excel'/>
as it works with the type 'input' i would like to use button of type submit
Add a value to the button, ie:
<button type="submit" class="btn btn-default btn-xs" name="Resume" value="true">
<span class="glyphicon glyphicon-download-alt"></span> Download Resume
</button>
You will have a $_POST['Resume'] with value "true" in your backend code.
Ofcourse something like: name="buttonName" value="Excel" and another button with name="buttonName" value="Resume" would make have a $_POST['buttonName'] with its value either being 'Excel' or 'Resume'.

Categories