How to insert multiple row from form in laravel 8 - php

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

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?

Send multiple data using checkbox via ajax

I have created a form which contains a table, the purpose of this is table is to select the desired fees to manage
<form method="POST" action="manage_fees.php" id="manage_form">
<table class="table table-striped table-bordered table-hover" id="table">
<thead>
<tr class="p-4">
<th scope="col">Select</th>
<th scope="col">School fees</th>
<th scope="col">Amount</th>
<th scope="col">type</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input check_amount" name="local_fees">
<label class="custom-control-label" for="check_amount"></label>
</div>
</td>
<td name="selected_fees">Local fees</td>
<td name="amount">200</td>
<td>University fees</td>
</tr>
</tbody>
</table>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" name="submit">Create</button>
</div>
</form>
Now I want to record it all data("fee name and total payment") that has checked in checkbox table and save in database using ajax.
Once the fees is selected it will display on the on input just like this
<div class="form-group">
label for="fs">Fees selected</label>
<input type="text" class="form-control" id="fs" name="fs" required disabled>
</div>
<div class="form-group">
<label for="tp">Total payment</label>
<input type="number" class="form-control" id="tp" name="tp" required disabled>
</div>
Now, the problem is that im having trouble saving the data that has been checked in table seems there is no data that has been pass in the input.
Sumbit.php
if(isset($_POST['submit'])){
$fs = $_POST['fs'];
$tp = $_POST['tp'];
$result = $connect->query("INSERT INTO manage_fees (fs,tp) VALUES ('$fs','$tp')") or die($connect->error());
}
Instead of this way how will send the data using ajax?

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

Save many values in one checkbox laravel

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

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