I have a form wrapped table with many repeating rows. The table's purpose is to allow editing of user accounts. Each row look like this:
<tr>
<td><input type="text" name="username" value="$row->username" /></td>";
<td><input type="text" name="fname" value="$row->fname" /></td>
<td><input type="text" name="lname" value="$row->lname" /></td>
<td><input type="email" name="email" value="$row->email" /></td>
<td><button type="submit" class="btn btn-primary" name="edituserbutton" value="$row->id" >Edit</button>";
</tr>
I build this table. I can change the names of each of the input elements, on a row
to row basis, so that they are made unique; name="username##" where ## = $row->id.
But I was wondering if the post could be scoped somehow to the row. I'm trying
to avoid having to build keys like this
$id = $_POST['edituserbutton'];
$user_name = $_POST['username'.$id];
Is this possible?
And also, concerning the presentation and editing of data, is this how it's generally done?
=========== update
Here's what I did. Works great. Thank you all:
$(document).ready(function() {
$('button[name=edituserbutton]').click(function(e) {
e.preventDefault();
var buttonvalue = $(this).attr("value");
alert("button value is " + buttonvalue);
// .. business here
return;
})
});
<tr>
<td><input type="text" name="username[$row->id]" value="$row->username" /></td>";
<td><input type="text" name="fname[$row->id]" value="$row->fname" /></td>
<td><input type="text" name="lname[$row->id]" value="$row->lname" /></td>
<td><input type="email" name="email[$row->id]" value="$row->email" /></td>
<td><button type="submit" class="btn btn-primary" name="edituserbutton" value="$row->id">Edit</button>";
</tr>
Then, in PHP
$id = $_POST['edituserbutton'];
$newUserName = $_POST['username'][$id];
If you also want to avoid submitting a lot of redundant data, you'll want to make each row a form on its own, like
<tr>
<form action="...">
<td><input type="text" name="username" value="$row->username" /></td>
<td><input type="text" name="fname" value="$row->fname" /></td>
<td><input type="text" name="lname" value="$row->lname" /></td>
<td><input type="email" name="email" value="$row->email" /></td>
<td>
<button type="submit" class="btn btn-primary" name="edituserbutton">Edit</button>
<input type="hidden" name="rowId" value="$row->id"/>
</td>
</form>
</tr>
Related
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>
I need some help...
I've created a form with a dynamic table in it. I would like to learn how to pass it through php and echo/print it to a php page to save the page as pdf.
I am stuck on this part. I've looked everywhere for some answers and I cant seem to be finding anything.
<form action="display_form.php" method="POST">
<table id="dataTable" width="auto" style="margin:-4px 0 0 0;" cellspacing="0px">
<tr>
<td style="width:20px;"><INPUT type="checkbox" name="chk" /></td>
<td><INPUT type="text" name="step" style="width:160px;" autocomplete="on" placeholder="step" required/></td>
<td><INPUT type="text" name="url" style="width:62px;" autocomplete="on" placeholder="url" required/></td>
<td><INPUT type="text" name="process" style="width:63px;" autocomplete="on" placeholder="process" required/></td>
<td>
<SELECT name="pass-fail" style="width:100px;">
<OPTION value="Pass">one</OPTION>
<OPTION value="Fail">two</OPTION>
</SELECT>
</td>
<td><INPUT type="text" name="comment" style="width:190px;" autocomplete="on" placeholder="Comment" required/></td>
</table>
<INPUT type="button" value="Add row" onclick="addRow('dataTable')" />
<INPUT type="button" value="Delete row" onclick="deleteRow('dataTable')" />
<INPUT type="submit" value="Send"/>
I have called the php file display_form.php where I would like to display the dynamic table data in the. The function to save it as a pdf is in there as well. But I am mainly after the printing of the form to the php file. I am having no luck there at all!
I'm not worried on posting the table to mysql. I have other ways where i don't need it in mysql
I would really appreciate the help
2 things
you have not closed tr
Next
Make all input elements as array. Then you can fetch them in your php file
<td style="width:20px;"><INPUT type="checkbox" name="chk[]" /></td>
<td><INPUT type="text" name="step[]" style="width:160px;" autocomplete="on" placeholder="step" required/></td>
<td><INPUT type="text" name="url[]" style="width:62px;" autocomplete="on" placeholder="url" required/></td>
<td><INPUT type="text" name="process[]" style="width:63px;" autocomplete="on" placeholder="process" required/></td>
<td>
<SELECT name="pass-fail[]" style="width:100px;">
<OPTION value="Pass">one</OPTION>
<OPTION value="Fail">two</OPTION>
</SELECT>
</td>
<td><INPUT type="text" name="comment[]" style="width:190px;" autocomplete="on" placeholder="Comment" required/></td>
In your php file, use foreach loop to get all the values.
$i = 0;
$array = array();
foreach ($_POST['step'] as $row) {
$array[$i]['step'] = $row;
$array[$i]['chk'] = isset($_POST['ckh'][$i]) ? 1 : 0;
$array[$i]['url'] = $_POST['url'][$i];
$i++;
//...//so on
}
print_R($array);
As I can see, the javascript addRow function should add another tr-row in the table (the closing is missing by the way).
Now you have in every table row inputs with the same names. That won't work.
You need to say, that they are arrays, to access them via $_POST['step'][0] and so on.
Just give them name="step[]" and the form will automatically count the index for you.
Use name='step[]',name='url[]',name='process[]' as input type for your add row functionality,
Now when you submit form you are getting array then store that array as below.
$step = $_REQUEST['step'];
$url = $_REQUEST['url'];
$process = $_REQUEST['process'];
Now use foreach loop.
foreach($step as $key => $row){
echo $step[$key];
echo $url[$key];
echo $process[$key];
}
Now format your data in html then generate pdf as per below link.
http://phptopdf.com/
if (isset($_POST)) {
$step = $_REQUEST['step'];
$url = $_REQUEST['url'];
$process = $_REQUEST['process'];
$pass_fail = $_REQUEST['pass-fail'];
$comment = $_REQUEST['comment'];
foreach ($step as $key => $row) {
{
?>
<table id="dataTable" width="auto" style="margin:-4px 0 0 0;" cellspacing="0px">
<tr>
<td><INPUT type="text" name="step[]" style="width:160px;" value="<?php echo $step[$key]; ?>"/></td>
<td><INPUT type="text" name="url[]" style="width:62px;" value="<?php echo $url[$key]; ?>"/></td>
<td><INPUT type="text" name="process[]" style="width:63px;" value="<?php echo $process[$key]; ?>"/>
</td>
<td><input name="pass-fail[]" style="width:100px;" value="<?php echo $pass_fail[$key]; ?>"/></td>
<td><INPUT type="text" name="comment[]" style="width:190px;" value="<?php echo $comment[$key]; ?>"/>
</td>
</tr>
</table>
<?php }
}
}
Working just fine now!!
here the table is displaying correctly
This is my table. I am developing billing application in core php mysql. Each row insert into table means need more memory, so I need to insert an one customer bill in a single row with all values separated by comma,
<table id="options-table" border="1">
<tbody><tr>
<th>S.NO</th>
<th>Product Name</th>
<th>Qty</th>
<th>Rate ($)</th>
<th>Total ($)</th>
<th>Option</th>
</tr>
<tr>
<td><span id="snum">1.</span></td>
<td><input type="text" name="productname[]" required="" class="ui-wizard-content valid"></td>
<!--<td><input type="text" style="width:80px;" name="itemcode" /></td> -->
<td><input type="text" class="qty ui-wizard-content valid" name="qty[]" required=""></td>
<td><input type="text" class="rate ui-wizard-content valid" name="rate[]" required=""></td>
<td><input type="text" class="tcost ui-wizard-content valid" name="tcost[]" readonly=""></td>
<td><input type="button" class="del btn-danger ui-wizard-content" value="Delete"></td>
</tr>
<tr>
<td><span id="snum">2.</span></td>
<td><input type="text" name="productname[]" required="" class="ui-wizard-content valid"></td>
<!--<td><input type="text" style="width:80px;" name="itemcode" /></td> -->
<td><input type="text" class="qty ui-wizard-content valid" name="qty[]" required=""></td>
<td><input type="text" class="rate ui-wizard-content valid" name="rate[]" required=""></td>
<td><input type="text" class="tcost ui-wizard-content" name="tcost[]" readonly=""></td>
<td><input type="button" class="del btn-danger" value="Delete"></td>
</tr><tr><td><span id="snum3">3.</span></td><td><input type="text" name="productname[]" required="" class="valid"></td> <td><input type="text" class="qty valid" name="qty[]" required=""></td> <td><input type="text" class="rate valid" name="rate[]" required=""></td> <td><input type="text" class="tcost" name="tcost[]" readonly=""></td><td><input type="button" class="del btn-danger" value="Delete"></td></tr><tr><td><span id="snum3">4.</span></td><td><input type="text" name="productname[]" required="" class="valid"></td> <td><input type="text" class="qty valid" name="qty[]" required=""></td> <td><input type="text" class="rate valid" name="rate[]" required=""></td> <td><input type="text" class="tcost" name="tcost[]" readonly=""></td><td><input type="button" class="del btn-danger" value="Delete"></td></tr><tr><td><span id="snum3">6.</span></td><td><input type="text" name="productname[]" required="" class="valid"></td> <td><input type="text" class="qty valid" name="qty[]" required=""></td> <td><input type="text" class="rate valid" name="rate[]" required=""></td> <td><input type="text" class="tcost valid" name="tcost[]" readonly=""></td><td><input type="button" class="add btn-success" value="Add More"></td></tr>
</tbody></table>
In the above table array values are insert into single row, PRODUCT NAME are separated with comma(,) same as QTY, RATE and TOTAL, can you suggest me.
Table structure:
S.NO Product Name Qty Rate ($) Total ($)
1. flowers 5 5 35
2. jasmine 6 5 30
3. rose 10 8 80
4. marigold 15 9 135
6.
This is example one , i tried this code work fine.
<?php
include 'connection/db_connection.php';
if(isset($_REQUEST['save'])) {
$product_name = implode(",", $_REQUEST["product"]);
print $product_name;
}
$query="insert into product (product_name) values('".$product_name."')";
$sql=mysql_query($query);
if($sql)
echo "success";
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form name="myform" method="post">
<input type="text" name="product[]" value="rose">
<input type="text" name="product[]" value="garlands">
<input type="text" name="product[]" value="marigold">
<input type="text" name="product[]" value="jasmine">
<input type="text" name="product[]" value="jasmine garlands">
<input type="submit" name="save" value="Save">
</form>
</body>
</html>
I want to ask about how to make a form invisible when a button clicked? So, if I have a button called "Hide" and a form with many button, textbox, etc.
Then when I click "Hide" button, it will hide all form and all things in form such as textbox, button, etc. I have google it but have no result. I accept answer with Jquery, JS, or php language because I'm using that language program.
Example my form is like this:
<form name="myform" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<table>
<tr>
<td>ID</td>
<td>:</td>
<td><input type="text" maxlength="15" name="clientid" /></td>
<td><input type="submit" name="cariclientid" value="Search" /></td>
<td width="50px"></td>
<td>ID</td>
<td>:</td>
<td><input type="text" maxlength="15" name="orderid" /></td>
<td><input type="submit" name="cariorderid" value="Search" /></td>
</tr>
<tr>
<td>No.</td>
<td>:</td>
<td><input type="text" maxlength="15" name="veh" /></td>
<td><input type="submit" name="carikendaraan" value="search" /></td>
<td></td>
<td>Nama Sopir</td>
<td>:</td>
<td><input type="text" maxlength="15" name="sopir" /></td>
<td><input type="submit" name="carisopir" value="Cari" /></td>
</tr>
<tr>
<td>Waktu Berangkat</td>
<td>:</td>
<td><input type="text" name="tglb" id="datetimepicker" /></td>
<td><input type="submit" name="cariberangkat" value="Cari" /></td>
<td></td>
<td>Waktu Pulang</td>
<td>:</td>
<td><input type="text" name="tglp" id="datetimepicker2" /></td>
<td><input type="submit" name="caripulang" value="Cari" /></td>
</tr>
</table>
</form>
maybe there's a way to make it invisible by a button?
You want something like this:
// code for only hide
$('#hide_button').on('click', function() {
$('form[name="myform"]').hide();
});
Demo for hide
and for toggle the form with a single button you can try:
$('#your_button').on('click', function() {
$('form[name="myform"]').toggle();
});
Demo for toggle
According to comment
To prevent the submission:
$('form[name="myform"]').submit(function(e) {
e.preventDefault();
// Your code
});
See here for .perventDefault()
I have a table as follows :
<table>
<thead>
<th>PRODUCT TYPE</th>
<th>QUANTITY</th>
<th>WEIGHT</th>
<th>WEIGHT PRICE</th>
<th>TOTAL</th>
</thead>
<tr>
<td>DRINKS</td>
<input type="hidden" name="product[]" value="1" />
<td><input type="text" name="qty[]" /></td>
<td>LITER</td>
<input type="hidden" name="unitPrice[]" value="34.92" />
<td><input type="text" name="total[]" readonly="readonly" /></td>
</tr>
<tr>
<td>DRY FOODS</td>
<input type="hidden" name="product[]" value="1" />
<td><input type="text" name="qty[]" /></td>
<td>KG</td>
<input type="hidden" name="unitPrice[]" value="16.30" />
<td><input type="text" name="total[]" readonly="readonly" /></td>
</tr>
<tr>
<td>FRESH FOOD</td>
<input type="hidden" name="product[]" value="1" />
<td><input type="text" name="qty[]" /></td>
<td>TON</td>
<input type="hidden" name="unitPrice[]" value="26.45" />
<td><input type="text" name="total[]" readonly="readonly" /></td>
</tr>
<tr>
<td>SWEET CONFECTIONARY</td>
<input type="hidden" name="product[]" value="1" />
<td><input type="text" name="qty[]" /></td>
<td>TON</td>
<input type="hidden" name="unitPrice[]" value="65.10" />
<td><input type="text" name="total[]" readonly="readonly" /></td>
</tr>
</table>
So, here is the question :
I want to check input value qty, if qty is filled then will take the related product value. But, the important things is no required to fill all those fields. Employees can fill one field or more!
Could anybody conduct to me to how can we handle this form with PHP?
Code like this is problematic because the elements of the product and qty arrays must be kept in sync, but the HTML4 (and HTML5) model for form submission does not say that these qty controls have to be successful. Therefore the browser might choose not to submit their values, which will mess up cross-array indexing.
The cleanest solution would be to explicitly specify the indexes for the arrays in each control's name:
<tr>
<td>DRINKS</td>
<input type="hidden" name="product[0]" value="1" />
<td><input type="text" name="qty[0]" /></td>
<td>LITER</td>
<input type="hidden" name="unitPrice[0]" value="34.92" />
<td><input type="text" name="total[0]" readonly="readonly" /></td>
</tr>
<tr>
<td>DRY FOODS</td>
<input type="hidden" name="product[1]" value="1" />
<td><input type="text" name="qty[1]" /></td>
<td>KG</td>
<input type="hidden" name="unitPrice[1]" value="16.30" />
<td><input type="text" name="total[1]" readonly="readonly" /></td>
</tr>
This way you can then do
foreach ($POST['product'] as $i => $val) {
if (!empty($POST['qty'][$i])) {
// the product has a nonzero quantity
}
}
Another option that you might want to consider is this:
<tr>
<td>DRINKS</td>
<input type="hidden" name="product[0][product]" value="1" />
<td><input type="text" name="product[0][qty]" /></td>
<td>LITER</td>
<input type="hidden" name="product[0][unitPrice]" value="34.92" />
<td><input type="text" name="product[0][total]" readonly="readonly" /></td>
</tr>
which would work along with
foreach ($POST['product'] as $product) {
if (!empty($product['qty'])) {
// the product has a nonzero quantity
}
}
You can assign unique index on array
<input type="hidden" name="product[1]" value="1" />
<td><input type="text" name="qty[1]" /></td>
...
<input type="hidden" name="product[2]" value="2" />
<td><input type="text" name="qty[2]" /></td>