Checkbox select all checkboxes Laravel - php

Here is my problem:
In PHP Laravel I have a foreach loop that displays messages on the screen including a checkbox. Every message has its own checkbox. I also have a checkbox at the top of all the messages. I would like to assign a function to that checkbox to check all the checkboxes. I know this question has been asked in the past, but unfortunately those answers didn't work for me. Does someone have a solution for me?
I'm using: Laravel, Inspinia, Bootstrap 4
Thanks in advance!
Here is my code:
#if(count($messages) > 0)
<table class="table table-hover">
<thead>
<tr>
<th> </th>
//select all checkboxes
<th><input type="checkbox" class=""/></th>
<th>#sortablelink('title', trans('messages.title'))</th>
<th>#sortablelink('sender_user_id', trans('messages.sender'))</th>
<th class="d-none d-sm-table-cell">#sortablelink('created_at', trans('messages.sent_at'))</th>
</tr>
</thead>
<tbody>
#foreach ($messages as $message)
<tr id="messagesTable">
<td><i class="{{ $message->read ? 'far fa-envelope-open' : 'fas fa-envelope' }}"></i></td>
//the checkboxes who need to be selected
<td class="project-title">
<div class="checkbox p1-1">
<input type="checkbox" id="message_{{$message->id}}" name="message" value="{{$message->id}}">
<label for="message_{{$message->id}}"></label>
</div>
</td>
<td class="project-title">
{{$message->title}}
</td>
<td class="project-title">
{{ $message->sender ? $message->sender->name : ''}}
</td>
<td class="project-title d-none d-sm-table-cell">
{{$message->created_at->format('d-m-Y H:i')}}
</td>
</tr>
#endforeach
</tbody>
</table>
#endif

you can do it in jquery
first step
change this
//select all checkboxes
<th><input type="checkbox" class=""/></th>
to this
//select all checkboxes
<th><input type="checkbox" class="check_all"/></th> //just added a class to this element
Second step
add class_name to all your <input type="checkbox">
i mean <input type="checkbox" id="message_{{$message->id}}" name="message" value="{{$message->id}}"> changed to <input type="checkbox" id="message_{{$message->id}}" name="message" value="{{$message->id}}" class="custom_name">
NOTE: be sure that all your checkboxes has one class_name instead the one witch if we click it others checked!
Third step
add this in footer
<script
src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"></script>
<script>
$(".check_all").on("click", function(){
$(".custom_name").each(function(){
$(this).attr("checked", true);
});
});
</script>
I HPOE THIS WORKS FOR YOU...

Related

PHP/MYSQL: Saving array data in table rows to mysql database using checkbox

I have an order table in my project. I am pulling the products from the database to this table using foreach. I show the quantity and unit price calculation information on the table instantly with jquery. I want to save the data in this table from the row selected with the checkbox to the database. I tried a few things with foreach but it looped so it created a new record for each row in the database. I want to print arrays with implode using commas between them. For example, the data in the rows entered in the quantity field in the table should be entered in the quantity field in the database as 1,2,3,4. If I have to explain briefly, I want to make an insert in one go.
Table:
Table & Form Code:
<form action="" method="POST">
<table class="table table-sm mb-3 text-center align-middle">
<thead>
<tr>
<th>Choose</th>
<th>Product Name</th>
<th width="137px">Quantity</th>
<th>Unit Price</th>
<th>Total Price</th>
</tr>
</thead>
<tbody>
<?php foreach($ProductTbl as $product){ ?>
<tr>
<th scope="row">
<div class="form-check form-check-success">
<input class="form-check-input" name="check[]" type="checkbox" value="
<?= $product-> productid">
</div>
</th>
<td>
<?= $product->productname ?>
</td>
<td>
<input class=" w-25 text-center quantity" type="text" name="quantity[]" value="0">
</td>
<td>
<input class="w-25 text-center unitprice" type="text" name="unitprice[]" value="
<?= $product->unitprice ?>" disabled="disabled"> €
</td>
<td>
<input class="w-25 text-center totalprice" type="text" name="totalprice[]" value="" disabled="disabled"> €
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="text-center">
<button class="btn btn-success col-md-2" type="submit" name="add">Offer Preview</button>
<button class="btn btn-warning col-md-1" type="reset">Reset</button>
<a href="#">
<button class="btn btn-danger col-md-1" type="button">Cancel</button>
</a>
</div>
</form>
//The code structure I tried
if(isset($_POST['add'])){
$check = $_POST['check'];
$quantity = implode(',', $_POST['quantity']);
$totalprice = implode(',', $_POST['totalprice']);
if (!empty($check)) {
foreach ($check as $chk => $value){
// I printed it for testing.
echo $value.' - product id in the checked checkbox<br>';
print_r($quantity);
print_r($totalprice);
}
}
}
How does this code work the way I want?

How to insert multiple row from form in laravel 8

I have a table in a form similar to this:
products
comments
product 1
comment 1
product 2
comment 2
Only comment field is writable where user can enter a comment regarding each product
Products are displayed from database (not in an input element)
I want, when user click on save button, each row is added in database as it is in the form (as pairs [product i, comment i])
I tried
foreach ($request->products as $key=>$product) {
$saveData = [
'product' => $request->product[$key],
'comment' => $request->comment[$key],
];
DB::table('prod_com')->insert($saveData);
}
in controller and
<form id="regForm" method="post" >
#csrf
<table>
<tr>
<th>Product</th>
<th>Comment</th>
</tr>
#foreach($prdcts as $products)
<tr>
<td>{{$products['product']}}</td>
<td>
<input type="text" class="form-control" name="comment[]" >
</td>
</tr>
</table>
in form but either data isn't added in database or i get a 405 error Method Not Allowed..
Any help is highly appreciated !
Edit :
Full html form
<form id="regForm" action="{{route('saved')}}" method="post" >
#csrf
<table class="table table-responsive table-condebsed table-hover">
<thead>
<tr>
<th scop="col"> Product </th>
<th class="col-md-6" scop="col"> Comment </th>
</tr>
</thead>
<tbody>
#foreach($prdcts as $products)
<tr>
<td>{{ $products['product'] }}</td>
<td>
<input type="text" class="form-control" name="comment[]" >
</td>
</tr>
#endforeach
</tbody>
</table>
<br>
<button type="submit" class="btn btn-sm btn-success">save</button>
</form>
& route :
Route::post('/saved', [App\Http\Controllers\ProdComController::class, 'submitData'])->name('saved');
Change html code as below
#foreach($prdcts as $key=>$products)
<tr>
<td>{{$products['product']}}</td>
<td>
<input type="hidden" class="form-control" name="product[{{ $key}}][product]" value="{{$products['id']}}">
<input type="text" class="form-control" name="product[{{ $key}}][comment]" >
</td>
</tr>
#endforeach
then in controller
DB::table('prod_com')->insert($request->product);
if you get any error then try to print $request like below
dd($request->all());
so you can check data format in the request.If still issue then let me know in the comment
you can use for loop while saving your data into database
for ($i = 0; $i < count($request->comment); $i++) {
$comment = Commet::create([
'comment' => $request->comment[$i],
]);
}

How to edit multiple field from one blade form

Hello I have a form like the below image-
The HTML code for this is-
<table style="width:100%" class="table table-striped">
<tr>
<td><strong>First Name</strong></td>
<td>Test Name</td>
<td><a class="btn btn-success" href="#">Edit</a></td>
</tr>
<tr>
<td><strong>Last Name</strong></td>
<td>Test Name</td>
<td><a class="btn btn-success" href="#">Edit</a></td>
</tr>
<tr>
<td><strong>Username</strong></td>
<td>test_username</td>
<td><a class="btn btn-success" href="#">Edit</a></td>
</tr>
</table>
Now what I am trying here is when I click on edit button I only need to be able to edit that specific field and store the updated value to my database only. All I want to know is, each time I click my form submit button only the updated field value should be passed to my controller method. I can pass my field name by a hidden input.
(I know I can keep a hidden input field in a form and change the field value by javascript. But I need to know if there is any better approach.)
Can I implement this using only one form tag in my Laravel blade? If yes how can I implement this without any touch in JS?
Okay, so you can do this with a css trick, look that I have added the input hidden with css, and instead of the a tag I used a label that is linked to the input with the for and id attributes, so when the user clicks on the label it focus on the input and triggers the css input:focus that shows the input and hide the name.
All is wrapped by a single form that if you add a <button type="input">Send</button> will send all to the controller
But you've mentioned that you just want to send the data that has been changed, unfortunately I don't think that this is possible without javascript, but you should set the value of the input to the original value, so when updated it will update to the same value, no problem at all, acctually almost all edit forms works that way
input {
opacity: 0;
width: 0;
}
input:focus {
visibility: visible;
opacity: 1;
width: 100px;
}
input:focus + span {
display: none;
}
<form>
<table style="width:100%" class="table table-striped">
<tr>
<td><strong>First Name</strong></td>
<td>
<input id="first_name" name="first_name" value="Test Name" />
<span>Test Name</span>
</td>
<td>
<label for="first_name" class="btn btn-success" href="#">Edit</label>
</td>
</tr>
<tr>
<td><strong>Last Name</strong></td>
<td>
<input id="last_name" value="Test Name" />
<span>Test Name</span>
</td>
<td>
<label for="last_name" class="btn btn-success" href="#">Edit</label>
</td>
</tr>
<tr>
<td><strong>Last Name</strong></td>
<td>
<input id="username" value="test_username" />
<span>test_username</span>
</td>
<td>
<label for="username" class="btn btn-success" href="#">Edit</label>
</td>
</tr>
</table>
</form>

Collect the values of checkboxes with jQuery

I have a website to predict football results. Each soccer match has three checkboxes.
1-First team win(1checkbox)
2-Second team win(1checkbox)
3-Equal(1checkbox)
There are 10 soccer matches on one page. So, on a page, I have 30 checkboxes. The user can select all three checkboxes for each football match.
I need jQuery code to work like this website:
https://web.archive.org/web/20100529040418/http://www.bwin90.com/
if you select two or three checkboxes in soccer match, the numbers are exponentially rising.
My html and php codes:
<form method="POST">
<div class="form-group mt-4">
<div class="w-100 p-3">
<table class="table">
<thead class="thead-dark">
<tr>
<th class="text-center" scope="col"></th>
<th class="text-center" scope="col">team1</th>
<th class="text-center" scope="col">equal</th>
<th class="text-center" scope="col">team2</th>
<th class="text-center" scope="col"></th>
</tr>
</thead>
<tbody>
<form>
<?php
$get_teams1="SELECT * FROM teams WHERE show_match=1 ORDER BY team_id DESC";
$get_teams2 = mysqli_query($conn, $get_teams1);
while($get_teams3=mysqli_fetch_assoc($get_teams2))
{
echo'
<tr>
<td class="text-right"><input type="checkbox" class="qty1 form-check-input" value="20" id="exampleCheck1"></td><td class="text-center">'.$get_teams3["mizban"].'</td>
<td class="text-center"><input type="checkbox" class="qty1 form-check-input" id="exampleCheck1"></td>
<td class="text-center">'.$get_teams3["mihman"].'</td><td class="text-right"><input type="checkbox" class="qty1 form-check-input" id="exampleCheck1"></td>
</tr>
';
}
?>
<input type="text" class="total" value="" />
</form>
</tbody>
</table>
<button type="button" class="btn btn-dark mt-2 w-100" name="register3" />
<h5>ثبت فرم</h5>
</button>
</div>
</div>
</form>
My jquery codes:
<script>
$(document).ready(function(){
$(document).on("change", ".qty1", function() {
var sum = 0;
$(".qty1").each(function(){
sum += +$('.qty1').val();
});
$(".total").val(sum);
});
});
</script>
At the end, I want to display the cost amount instantly by selecting checkboxes.
you May try something like this
Html Code
<table class="table table-bordered" id="filltable">
<tr>
<td>demo <input type="checkbox" id="chk1" value="10" onchange="add(1,10)"/></td>
<td>demo1 <input type="checkbox" id="chk2" value="10" onchange="add(2,10)" /></td>
<td>demo2 <input type="checkbox" id="chk3" value="10" onchange="add(3,10)"/></td>
<td>demo3 <input type="checkbox" id="chk4" value="10" onchange="add(4,10)"/></td>
</tr>
</table>
and Jquery Code:
var sum = 0;
function add(id,value) {
if ($('#chk' + id).prop('checked') == true) {
sum= sum + value
}
else {
sum= sum - value
}
alert(sum);
}

using a div as a radio button

<div>
<table class='accor'>
<tr>
<td>Roll No.</td>
<td>12801</td>
</tr>
<tr>
<td>Name</td>
<td>XXXX</td>
</tr>
<tr>
<td>Class</td>
<td>MS</td>
</tr>
</table>
</div>
i want the above code as a radio button. the above code is running inside a php while loop with the data supplied from a database. (i also need to use checked=checked to match a predefined value.)
i am using the jquery ui buttonset style with the radio.
how can i achieve that?
$().buttonset takes all the input type='radio' elements inside the selector and makes a stylized button set out of them. For each of these elements, the associated label is placed on the display of the resulting button. So to have buttons with labels that aren't just a single line of text, just place the contents inside the label.
<form>
<div id="radio">
<input type="radio" id="radio1" name="radio" />
<label for="radio1">
<div>
<table class='accor'>
<tr>
<td>Roll No.</td>
<td>12801</td>
</tr>
<tr>
<td>Name</td>
<td>XXXX</td>
</tr>
</table>
</div>
</label>
<input type="radio" id="radio2" name="radio" checked="checked" />
<label for="radio2"><div>
<table class='accor'>
<tr>
<td>Roll No.</td>
<td>12801</td>
</tr>
<tr>
<td>Name</td>
<td>XXXX</td>
</tr>
</table>
</div>
</label>
</div>
</form>
JS:
$(function() {
$("#radio").buttonset();
});
try following (embed your code as you want):
$('.accor td').on("click",function(){
$('td').css('background-color','#eee');
var rowNumber = $(this).closest('tr').index();
setSelected(rowNumber);
});
function setSelected(rowNumber){
$('tr').eq(rowNumber).find('td').css('background-color','#ccc');
}
setSelected(2);
working fiddle here: http://jsfiddle.net/ZEwpE/2/
i hope it helps.
or another option to do that is:
$('.accor td').on("click",function(){
$('td').css('background-color','#eee');
var tr = $(this).parent();
$(tr).children().css('background-color','#ccc');
});
Get click event
Get parent (in your case it's )
Set css style to all childs of our
But it's only alternate (very similar) method:
jsfiddle

Categories