Multiple Form Submit - Laravel - php

I have my edit.blade.php file as below mentioned
<table class="table">
<thead>
<tr>
<th>Vendor</th>
<th>Item</th>
<th>Qty</th>
<th>Rate</th>
<th>Total</th>
<th>Description</th>
<th>Job No</th>
</tr>
</thead>
<tbody>
#foreach ($po_items as $po_item)
<tr>
<td><input type="text" class="form-control" name="vendor_name[]" id="vendor_name" value="{{$po_item->cost_item->vendor_company->name}}" readonly></td>
<td><input type="text" class="form-control" name="item_name[]" id="item_name" value="{{$po_item->cost_item->item->name}}" readonly></td>
<td><input type="text" class="form-control" name="qty[]" id="qty" value="{{$po_item->qty}}"></td>
<td><input type="text" class="form-control" name="rate[]" id="rate" value="{{$po_item->rate}}"></td>
<td><input type="text" class="form-control" name="total[]" id="total" value="{{$po_item->qty * $po_item->rate}}"></td>
<td><input type="text" class="form-control" name="description[]" id="description" value="{{$po_item->description}}"></td>
<td><input type="text" class="form-control" name="job_id[]" id="job_id" value="{{$po_item->cost_item->job_id}}" readonly></td>
<td>
<form action="{{action('Employee\POItemController#destroy', $po_item->id)}}" method="post">
{{csrf_field()}}
{{method_field('DELETE')}}
<button class="btn btn-danger" type="submit">Delete</button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
<form action="{{ route('employee.poitem.update', $vendor_company_id )}}" method="post">
{{csrf_field()}}
{{method_field('PATCH')}}
#foreach ($po_items as $po_item)
<input type="hidden" name="cost_item_id[]" value="?">
<input type="hidden" name="qty[]" value="?">
<input type="hidden" name="rate[]" value="?">
<input type="hidden" name="description[]" value="?">
#endforeach
<button type="submit" class="btn btn-primary">Update</button>
</form>
Since there are two forms in the same page, i find it hard to pass new form values to the update function. I read on using the same session to solve this issue, but it wasn't clear enough for me to proceed with it

Instead of Making delete form you can do it with anchor tag. I don't know it is correct way or not but it works fine.
Delete
Hope this helps :)

Your input id values are incorrect. In HTML, an id should be unique across all HTML tags. When you make a foreach :
#foreach ($po_items as $po_item)
<tr>
<td><input type="text" class="form-control" name="vendor_name[]" id="vendor_name" value="{{$po_item->cost_item->vendor_company->name}}" readonly></td>
</tr>
#endforeach
you're assigning all inputs vendor_name to same id="vendor_name, which is W3C incorrect. You could use instead
#foreach ($po_items as $po_item)
<tr>
<td><input type="text" class="form-control" name="vendor_name[]" id="vendor_name_{{$po_item->id}}" value="{{$po_item->cost_item->vendor_company->name}}" readonly></td>
</tr>
#endforeach
But that's not the real problem here, you need upon submitting the UPDATE Form to read each input data and pushes it in hidden inputs.
Do you use vanilla Javascript or JQuery? With JQuery you can achieve it easier, you must put some scripts on the page that contains the forms. The idea is to find each input value for like qty, push them into array and convert that array to JSON string and put that JSON string as value to hidden qty input. Try this codes :
<table class="table">
<thead>
<tr>
<th>Vendor</th>
<th>Item</th>
<th>Qty</th>
<th>Rate</th>
<th>Total</th>
<th>Description</th>
<th>Job No</th>
</tr>
</thead>
<tbody id="table_inputs">
#foreach ($po_items as $po_item)
<tr>
<td><input type="text" class="form-control" name="vendor_name[]" id="vendor_name_{{$po_item->id}}" value="{{$po_item->cost_item->vendor_company->name}}" readonly></td>
<td><input type="text" class="form-control" name="item_name[]" id="item_name_{{$po_item->id}}" value="{{$po_item->cost_item->item->name}}" readonly></td>
<td><input type="text" class="form-control" name="qty[]" id="qty_{{$po_item->id}}" value="{{$po_item->qty}}"></td>
<td><input type="text" class="form-control" name="rate[]" id="rate_{{$po_item->id}}" value="{{$po_item->rate}}"></td>
<td><input type="text" class="form-control" name="total[]" id="total_{{$po_item->id}}" value="{{$po_item->qty * $po_item->rate}}"></td>
<td><input type="text" class="form-control" name="description[]" id="description_{{$po_item->id}}" value="{{$po_item->description}}"></td>
<td><input type="text" class="form-control" name="job_id[]" id="job_id_{{$po_item->id}}" value="{{$po_item->cost_item->job_id}}" readonly></td>
<td>
<form action="{{action('Employee\POItemController#destroy', $po_item->id)}}" method="post">
{{csrf_field()}}
{{method_field('DELETE')}}
<button class="btn btn-danger" type="submit">Delete</button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
<form id="form_update_poitem" action="{{ route('employee.poitem.update', $vendor_company_id )}}" method="post">
{{csrf_field()}}
{{method_field('PATCH')}}
#foreach ($po_items as $po_item)
<input type="hidden" name="cost_item_id[]" >
<input type="hidden" name="qty[]" >
<input type="hidden" name="rate[]" >
<input type="hidden" name="description[]" >
#endforeach
<button type="submit" class="btn btn-primary">Update</button>
</form>
<script>
$(document).ready(function() {
console.log('document is ready, proceed to form update codes');
// find button form
var button = $('#form_update_poitem button');
// add onclick event
button.on('click', function(e){
// find input array input values, turn them into array and pass that array to Hidden Input array
var itemArr = [];
$("#table_inputs input[type='item_name']").each(function(){
itemArr.push($(this).val());
});
console.log(itemArr);
$("#form_update_poitem input[type='cost_item_name']").val(JSON.stringify(itemArr));
var qtyArr = [];
$("#table_inputs input[type='qty']").each(function(){
qtyArr.push($(this).val());
});
console.log(qtyArr);
$("#form_update_poitem input[type='qty']").val(JSON.stringify(qtyArr));
var rateArr = [];
$("#table_inputs input[type='rate']").each(function(){
rateArr.push($(this).val());
});
console.log(rateArr);
$("#form_update_poitem input[type='rate']").val(JSON.stringify(rateArr));
var descriptionArr = [];
$("#table_inputs input[type='description']").each(function(){
descriptionArr.push($(this).val());
});
console.log(descriptionArr);
$("#form_update_poitem input[type='description']").val(JSON.stringify(descriptionArr));
});
});
</script>

Related

Submit Data Array With JqueryAutoCalc

Submit to Database Success But Value Of Debit And Kredit are 0 and 1
<table>
<tr>
<td>
<input class="form-control" name="debit[]" type="text" placeholder="Debit" autocomplete="off" required />
</td>
<td>
<input class="form-control" name="Kredit[]" type="text" placeholder="Kredit" autocomplete="off" required />
</td>
</tr>
<tr>
<th>Total</th>
<th colspan="2">
<input class="form-control" name="total" type="text" jAutoCalc="{debit[]} + {kredit[]}" />
</th>
</table>
This is the error i receive
Parsing error:unrecognied value
(Error:valueNotParsed):<*>{debit[]}+{kredit[]}

Add Delete row in two different table

The rows are getting added in upper table only even though table id is different
The functionality shpuld work as I have included different ID for different table
Please heklpme to identify the error
THE JS is below
$(document).ready(function () {
$('#myTable').on('click', 'input[type="button"]', function () {
$(this).closest('tr').remove();
});
$('p input[type="button"]').click(function () {
$('#myTable').append('<tr><td><input type="text" class="form-control" name="aname[]" value="" placeholder="Input Name" /> </td><td><input type="amount" class="form-control" name="aamount[]" value="" placeholder="Input Value" /></td><td><input type="number" class="form-control" name="apriority[]" value="" placeholder="Input Value" /></td><td><input type="button" class ="btn btn-danger" value="x Delete" /></td></tr>')
});
$('#myTable1').on('click', 'input[type="button"]', function () {
$(this).closest('tr').remove();
});
$('p input[type="button"]').click(function () {
$('#myTable1').append('<tr><td><input type="text" class="form-control" name="dname[]" value="" placeholder="Input Name" /> </td><td><input type="amount" class="form-control" name="damount[]" value="" placeholder="Input Value" /></td><td><input type="number" class="form-control" name="dpriority[]" value="" placeholder="Input Value" /></td><td><input type="button" class ="btn btn-danger" value="x Delete" /></td></tr>')
});
});
Below is HTML
<div class="form-group">
<label for="grade">Grade</label>
<select class="form-control required alphanum" name="grade" id="grade" required>
<option value="">Please select...</option>
<?php fetch_grade($conn);
?>
</select>
</div>
<div class="form-group">
<label for="name">Basic Salary</label>
<input type="text" name="basic" id="basic" class="form-control" />
</div>
<div class="form-group">
<label for="overtime">Overtime Rate(Per Hour)</label>
<input type="text" name="overtime" id="overtime" class="form-control" />
</div>
Allowances
<br>
<p> <input type="button" class="btn btn-primary" value="+ Add Allowance"></p>
<table id="myTable" class="table table-condensed table-hover table-bordered table-hover table-responsive table-striped">
<thead>
<tr>
<th>Allowance Name</th>
<th>Amount</th>
<th>Display Priority</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" class="form-control" name="aname[]" value="" placeholder="Input Name" /> </td>
<td><input type="amount" class="form-control" name="aamount[]" value="" placeholder="Input Value" /></td>
<td><input type="number" class="form-control" name="apriority[]" value="" placeholder="Input Value" /></td>
<td><input type="button" class ="btn btn-danger" value="x Delete" /></td>
</tr>
</tbody>
</table>
Deductions
<p> <input type="button" class="btn btn-primary" value="+ Add Deduction"></p>
<table id="myTable1" class="table table-condensed table-hover table-bordered table-hover table-responsive table-striped">
<thead>
<tr>
<th>Deduction Name</th>
<th>Amount</th>
<th>Display Priority</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" class="form-control" name="dname[]" value="" placeholder="Input Name" /> </td>
<td><input type="amount" class="form-control" name="damount[]" value="" placeholder="Input Value" /></td>
<td><input type="number" class="form-control" name="dpriority[]" value="" placeholder="Input Value" /></td>
<td><input type="button" class ="btn btn-danger" value="x Delete" /></td>
</tr>
</tbody>
</table>
I am not sure where I am going wrong? I have searched various forum but didnt come with accurate solutions and was wonderingif someone canthrow light on this and how can I optmisie it
This selector bind some function twice:
$('p input[type="button"]')
which means it will only run for one table.
Add some class to the <p> element
<p class="btnMyTable"> <input type="button"
<p class="btnMyTable1"> <input type="button"
And bind to it:
$('p.btnMyTable input[type="button"]')
$('p.btnMyTable1 input[type="button"]')

Add row to table and calculate rows value

<div class="col-xs-12 col-md-10" style="padding:0px 100px 0px 50px; color:blue;">
<form action="#" style="color:blue;" method="POST" >
<div class="well well-sm" >
<table border="collapse; border-spacing:5px;" style="background-color:pink;">
<thead>
<th>Stock ID</th>
<th>Medicine Name</th>
<th>Quantity Unit</th>
<th>Unit Price</th>
<th>Selling Price</th>
<th>Sold Quantity</th>
<th>Total Price</th>
<th><input type="button" value="+" id="add" class="btn btn-primary"></th>
</thead>
<tbody class="detail">
<tr>
<td><input type="text" class="form-control" id="si" name="si"></td>
<td><input type="text" class="form-control" id="mn" name="mn" value="<?php
include_once('classes/class.select.php');
$bill = new select();
$value = $bill ->billing($_POST['si']);
foreach($value as $row){
echo $row["medi_name"];
}
?>"></td>
<td><input type="text" class="form-control" id="qu" name="qu" value="<?php echo $row["quantity_unit"];?>"></td>
<td>
<input type="text" class="form-control" id="up" name="up" value="<?php echo $row["unit_price"]; ?>"></td>
<td>
<input type="text" class="form-control" id="sp" name="sp" value="<?php echo $row["selling_price"];?>"></td>
<td><input type="text" class="form-control" id="t" name="t" ></td>
<td><input type="text" class="form-control" id="ls" name="ls" ></td>
<td><input type="submit" class="btn btn-success" value="Bill" name="savesel"><br></td>
</tr>
</tbody>
</table>
</div>
</form>
</div>
This is my table of billing system of medicine. When we give stock Id, the other columns are automatically fill according to relevant stock Id. I want to add some more rows to this table like this. But when I click on bill button on second row, whole row will be disappeared. I also want to calculate whole total price of the end.
<script>
function addnew(){
var n=($('.detail tr').length-0)+1;
var tr =
'<tr>' +
'<td><input type="text" class="form-control" id="si" name="si"></td>'+
'<td><input type="text" class="form-control" id="mn" name="mn" value="<?php
include_once('classes/class.select.php');
$bill = new select();
$value = $bill ->billing($_POST['si']);
foreach($value as $row){
echo $row["medi_name"];
}
?>"></td>'+
'<td><input type="text" class="form-control" id="qu" name="qu" value=["quantity_unit"]></td>'+
'<td><input type="text" class="form-control" id="up" name="up" value=["unit_price"]></td>'+
'<td><input type="text" class="form-control" id="sp" name="sp" value=["selling_price"]></td>'+
'<td><input type="text" class="form-control" id="t" name="t" ></td>'+
'<td><input type="text" class="form-control" id="ls" name="ls" ></td>'+
'<td><input type="submit" class="btn btn-success" value="Bill" name="savesel"><br></td>'+
'</tr>';
$('.detail').append(tr);
}
$(function(){
$('#add').click(function(){
addnew();
});
});
</script>
$("#cloneTable tfoot #addNew").on("click", function() {
var clonedRow = $('#cloneTable tbody tr:first').clone();
clonedRow.find('input').val('');
$('#cloneTable tbody').append(clonedRow) ;
});

Why Does my $_Post only appear to Store the Last Value Inputted?

I'm Tring to use Var_dump or print_r to Debug my code before I write the php code to insert it into a Database. When I use these methods I only get the Last value of my Form, I've searched why and can't quite find the Answer, I changed name="something" to name="something[]", that seems to be the most common mistake, but that didn't seem to change anything.
So I have this HTML Code on my form page:
<form method="post" action="phptestpage.php">
<table>
<tr>
<th>Job Number</th>
<th>Timesheet #</th>
<th>Work Date</th>
<th>Shift</th>
</tr>
<tr>
<td><input type="text" name="job_number"></td>
<td><input type="text" name="timesheet"></td>
<td><input type="date" name="work_date"></td>
<td><select>
<option value="days">Days</option>
<option value="nights">Nights</option>
</select></td>
</tr>
</table>
<table>
<tr>
<th id="work_done">Work Done</th>
<th id="equipment_used">Equipment Used</th>
</tr>
<tr>
<td><textarea type="text" name="work_done" id="work_done_input"></textarea></td>
<td><textarea type="text" name="equipment_used" id="equipment_used_input"></textarea></td>
</tr>
</table>
<table id="time_table">
<tr>
<th>Last Name</th>
<th>First Name</th>
<th>Rate</th>
<th class="time">ST</th>
<th class="time">TH</th>
<th class="time">DT</th>
<th class="time">EX-ST</th>
<th class="time">EX-TH</th>
<th class="time">EX-DT</th>
</tr>
<tr class="blank_row">
<td><input type="text" name="last_name[]" class="last_name"></td>
<td><input type="text" name="first_name"></td>
<td><input type="text" name="rate"></td>
<td><input type="number" name="st" class="time_input" value="0"></td>
<td><input type="number" name="th" class="time_input" value="0"></td>
<td><input type="number" name="dt" class="time_input" value="0"></td>
<td><input type="number" name="ex_st" class="time_input" value="0"></td>
<td><input type="number" name="ex_th" class="time_input" value="0"></td>
<td><input type="number" name="ex_dt" class="time_input" value="0"></td>
</tr>
<tr>
<td><input type="text" name="last_name[]" class="last_name"></td>
<td><input type="text" name="first_name"></td>
<td><input type="text" name="rate"></td>
<td><input type="number" name="st" class="time_input" value="0"></td>
<td><input type="number" name="th" class="time_input" value="0"></td>
<td><input type="number" name="dt" class="time_input" value="0"></td>
<td><input type="number" name="ex_st" class="time_input" value="0"></td>
<td><input type="number" name="ex_th" class="time_input" value="0"></td>
<td><input type="number" name="ex_dt" class="time_input" value="0"></td>
</tr>
</table>
<input type="submit" value="Submit" name="submit">
</form>
and I'm using this code to try to See what values it's outputting:
<?php
$names = $_POST['last_name'];
var_dump($names);
?>
I've tried a few variations not knowing what i'm really doing, including putting [] inside the names variable value like $_POST['last_name[]'] to try to match the variable, I've made mistakes like that before.
You need to wrap you inputs in a <form> element with the correct action and method.
<form action="/path/to/file.php" method="POST">
<input type="text" name="last_name[]" class="last_name">
</form>
By doing this you should then be able to return an array using $_POST['last_name']
EDIT
I have tested this locally with a simplified version of the form and the following will work:
<form method="POST">
<input type="text" name="last_name[]">
<input type="text" name="last_name[]">
</form>
This will return $_POST['last_name'] = array(0 => 'test1', 1 =>'test2'). I think that your problem is not with what you have named your variables but something else in your script. I would recommend commenting out sections and debugging as you go.

ContentType: multipart/form-data when POSTing access_token to a resource

I need Your help, i am implementing OAuth2 Services getting library from https://github.com/bshaffer/oauth2-server-php
Every thing is woking fine but when i used enctype="multipart/form-data" in form getting error The access token provided is invalid
My Form
<form action="contact.php" method="post" enctype="multipart/form-data" name="contact" id="conact">
<table>
<tbody>
<tr>
<td>Image</td>
<td><input type="file" name="image" id="image" value="" /></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="name" id="name" maxlength="25" value="" /></td>
</tr>
<tr>
<td>Status</td>
<td><input type="text" name="status" id="status" maxlength="25" value="" /></td>
</tr>
<tr>
<td>Access Token</td>
<td><input type="text" name="access_token" id="access_token" value="" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" id="submit" value="Submit" /></td>
</tr>
</tbody>
</table>
</form>
But When i remove it enctype="multipart/form-data" everthing is working file but image is not upload.
My contact.php have
if (!$server->verifyResourceRequest(OAuth2\Request::createFromGlobals())) {
$response[]=$server->getResponse()->send();
die;
}
This Code is Checking Valid and Invalid Access Token
Please Help.
Thanks
I am not get these answer yet so i use the alternative method for this I passed the access token in action Like this
<form action="contact.php?access_token=" method="post" enctype="multipart/form-data" name="contact" id="conact">
<table>
<tbody>
<tr>
<td>Image</td>
<td><input type="file" name="image" id="image" value="" /></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="name" id="name" maxlength="25" value="" /></td>
</tr>
<tr>
<td>Status</td>
<td><input type="text" name="status" id="status" maxlength="25" value="" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" id="submit" value="Submit" /></td>
</tr>
</tbody>
</table>
</form>
It working fine.
I too had the similar problem solved by doing this
<form action="contact.php?access_token=MY_TOKEN_HERE" method="post" enctype="multipart/form-data" name="contact" id="conact">
<table>
<tbody>
<tr>
<td>Image</td>
<td><input type="file" name="image" id="image" value="" /></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="name" id="name" maxlength="25" value="" /></td>
</tr>
<tr>
<td>Status</td>
<td><input type="text" name="status" id="status" maxlength="25" value="" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" id="submit" value="Submit" /></td>
</tr>
</tbody>
</table>
</form>
You can also check this issue on bsaffers library issue
This is also mentioned on oauth library

Categories