Save many values in one checkbox laravel - php

I have a table that shows all files from public/folder,
with format name 1_CCA-2018-0182224-720422085426
1_CCA-2018-0182224 this is ACCOUNT ID
720422085426 this is ID NO
when user check the checkbox I want to save filename to 2 column (ACCOUNT ID & ID NO)
I Use hidden to get ID NO value, but the problem is, the value of ID NO is not match with filename,
if i thick the last record, ID NO will save ID NO before the last record,
but if thick the first record, ID NO save correctly,
can anyone help me?
<table id="dataTable" class="table table-bordered table-condensed table-hover table-striped" width="100%" border="1">
<thead>
<tr>
<th width="5%">No</th>
<th width="45%">Account ID & ID No</th>
<th data-checkbox="true" width="5%"></th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
<tr>
<td>1</td>
<td>123</td>
<td>
<input type="checkbox" name="account_id[]" value="{{ substr($file,10,18) }}">
<input type="hidden" name="file[]" value="{{$file}}">
<input type="hidden" name="id[]" value="{{ substr($file,29,12) }}">
</td>
</tr>
<tr>
<td>2</td>
<td>1234</td>
<td>
<input type="checkbox" name="account_id[]" value="{{ substr($file,10,18) }}">
<input type="hidden" name="file[]" value="{{$file}}">
<input type="hidden" name="id[]" value="{{ substr($file,29,12) }}">
</td>
</tr>
<?php $i++; ?>
</tbody>
</table>

You should be able to synchronize the data retrieval by using the following approach:
foreach($request->id as $key => $value) {
$id = $value;
$file = $request->file[$key];
$accountId = $request->account_id[$key] ?? null;
if ($accountId) {
// do stuff if checkbox was ticked
} else {
// do stuff if checkbox wasn't ticked
}
}

Related

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],
]);
}

Checkbox select all checkboxes Laravel

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...

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

take multiple input from user on dynamic post name

Please check the codes bellow. I need to take each products quantity value input form user. But the products/id are dynamic and coming form database. how can i receive and identify which products quantity value will be what submitted by user? i mean since product is multiple/dynamic coming form database then so i cant get them by $_POST['id']. whats the solution for this case then?
<form method="post">
<table class="table table-hover">
<thead>
<tr>
<th>Stock Available</th>
<th>Product Name</th>
<th>Enter Order Quantity</th>
</tr>
</thead>
<tbody>
<?php
$q= mysqli_query($conn,"SELECT * FROM products");
while ($row=mysqli_fetch_array($q)) {
$id = $row['id'];
$product_name = $row['product_name'];
$available_stock = $row['available_stock'];
echo '
<tr>
<td>'.$available_stock.'</td>
<td>'.$product_name.'</td>
<td><input type="number" min="1" class="form-control" name="qty[]"></td>
<input type="hidden" name="id[]" value="'.$id.'">
<tr>
';
}
?>
</tbody>
</table><br><br>
<button type="submit" class="btn btn-success" name="submit_next_order2">Confirm This Order</button>
</form>
<?php
if($_POST['submit_next_order2'])
{
print_R($_POST);die;
}
?>
Please update your html.
<?php
$q= mysqli_query($conn,"SELECT * FROM products");
while ($row=mysqli_fetch_array($q)) {
$id = $row['id'];
$product_name = $row['product_name'];
$available_stock = $row['available_stock'];
echo '
<tr>
<td>'.$available_stock.'</td>
<td>'.$product_name.'</td>
<td><input type="number" min="1" class="form-control" name="qty['.$id .'][]"></td>
<input type="hidden" name="id['.$id .'][]" value="'.$id.'">
<tr>
';
}
?>

Validate dynamically created text inputs in jQuery

What I presently have is this code which displays all the invoices under a specific catalogue. On change of the catalogue dropdown, the table should be populated based on the selected dropdown value. The table displays the invoices pertaining to the selected catalogue with 3 textboxes for each recordset. And what I need is to validate those textboxes. Lets say there are 3 invoices/records for a specific catalogue, then there should be 9 textboxes.
The View Page contains:-
<script type="text/javascript">
$(document).ready(function(){
$('#catalogue').change(function(){
$.post('../controller/valuation.php', {catalogue:valuation_form.catalogue.value},
function(result){
$('#valuationResult').html(result).show();
})
});
});
</script>
<form action="" id="valuation_form" name="valuation_form">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="16%">Catalogue Code</td>
<td width="15%">
<select name="catalogue" id="catalogue" style="width:75px;">
<option></option>
<?php
require_once '../model/catalogue.php';
#$result=Catalogue::getAllCatalogues();
while($value=mysql_fetch_assoc($result)){
?>
<option value="<?php echo $value['catalogue_id']; ?>">
<?php echo $value['catalogue_code'] ?>
</option>
<?php } ?>
</select>
</td>
</tr>
</table>
</form>
<div class="atable">
<form action="../controller/valuation.php" method="POST" enctype="multipart/form-data">
<div id="valuationResult"></div>
</form>
</div>
The Controller Page contains:-
function getValuationDetails(){
require_once '../model/sales.php';
$catalogue=$_POST['catalogue'];
$obj=new Sales();
$result=$obj->getValuationEntryLotsForCatalogue($catalogue);
#$num=mysql_num_rows(#$result);
if($num>0){
$table='<table id="tabled" class="tftable" border="1">
<thead>
<tr>
<th style="display:none" width="0%">INVOICE ID</th>
<th width="6%">LOT #</th>
<th width="15%">SELLING MARK</th>
<th width="9%">INVOICE #</th>
<th width="8%">PACKAGES</th>
<th width="8%">GRADE</th>
<th width="13%">NET WEIGHT(Kg)</th>
<th style="text-align:center">DESCRIPTION</th>
<th width="11%" style="text-align:center;">VALUATION </th>
</tr>
</thead>';
while($value=mysql_fetch_assoc($result)){
$table.='<tr>
<td style="display:none">
<input type="text" id="invoice" name="invoice[]" value="'.$value['invoice_id'].'"/>
</td>
<td>'.$value['lot_no'].'</td>
<td>'.$value['selling_mark'].'</td>
<td>'.$value['ref_invoice_no'].'</td>
<td>'.$value['no_of_packages'].'</td>
<td>'.$value['grade_desc'].'</td>
<td style="text-align:right;">'.$value['total_net_qty'].' </td>
<td>
<textarea style="width:270px;max-width:270px;" class="description" name="description[]"></textarea>
</td>
<td>
<input type="text" class="value1" name="value1[]" size="2">
<input type="text" class="value2" name="value2[]" size="2">
</td>
</tr>';
}
$table.='<tr><td colspan="9" style="text-align:right">
<input type="hidden" name="action" value="valuation_entry"/>
<input type="submit" name="save" id="save" value="Save"/>
<input type="reset" value="Clear"/>
</td></tr>';
echo $table;
}
else{
echo "";
}
}
The three textboxes/textareas are as follows:-
1. Description- textarea that allows only letters.
2. Value1- textbox that allows only numbers.
3. Value1- textbox that allows only numbers.
*All the 3 cannot be empty.
I have tried all sorts of options to try this but unfortunately I cannot figure out what and where I am missing.
Any help is very much appreciated!!!
Use class "required" to mark the fields that cannot be empty.
Then on Jquery you should loop through these elements:
var error = false;
$('.required').each(function(i){
if(!$(this).val()){
$(this).css('border', '2px solid red');
error = true;
}else{
$(this).css('border','1px solid #BBBBBB');
}
});
if(error) {
alert('All red fields are required!!!!!');
}

Categories