How to handle multiple same form fields shortway in PHP? - php

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>

Related

How Update mysql database from form with many row chekecd that contain many data in each row

in table:
<form action="" method="post">
<table>
<tr>
<td>ID</td>
<td>Customer Name</td>
<td>Price</td>
<td>Control</td>
</tr>
<tr>
<td><input type="text" value="111" name="userID" /></td>
<td><input type="text" value="john" name="fname" /></td>
<td><input type="text" value="1000" name="price" /></td>
<td><input type="checkbox" value="120" name="checkbox[]" /></td>
</tr>
<tr>
<td><input type="text" value="115" name="userID" /></td>
<td><input type="text" value="mike" name="fname" /></td>
<td><input type="text" value="800" name="price" /></td>
<td><input type="checkbox" value="120" name="checkbox[]" /></td>
</tr>
<tr>
<td><input type="text" value="98" name="userID" /></td>
<td><input type="text" value="tetra" name="fname" /></td>
<td><input type="text" value="125" name="price" /></td>
<td><input type="checkbox" value="120" name="checkbox[]" /></td>
</tr>
</table>
<button type="submit">Update Database</button>
</form>
when checked one or more checkbox then press button how update mySql table rows contain name and price together?
answers that i see in this site or other sites can solve this problem but with only one record in each row.
in this case each row contain more than one data such as username and price and ett that must update in mysql table.
thanks in advance
If I understood correctly then perhaps the following might be of interest / help.
Firstly modify the form elements to use the same syntax as you assigned to the checkbox field - an unusual name however.
<form action="" method="post">
<table>
<tr>
<td>ID</td>
<td>Customer Name</td>
<td>Price</td>
<td>Control</td>
</tr>
<tr>
<td><input type="text" value="111" name="userID[]" /></td>
<td><input type="text" value="john" name="fname[]" /></td>
<td><input type="text" value="1000" name="price[]" /></td>
<td><input type="checkbox" value="120" name="checkbox[]" /></td>
</tr>
<tr>
<td><input type="text" value="115" name="userID[]" /></td>
<td><input type="text" value="mike" name="fname[]" /></td>
<td><input type="text" value="800" name="price[]" /></td>
<td><input type="checkbox" value="120" name="checkbox[]" /></td>
</tr>
<tr>
<td><input type="text" value="98" name="userID[]" /></td>
<td><input type="text" value="tetra" name="fname[]" /></td>
<td><input type="text" value="125" name="price[]" /></td>
<td><input type="checkbox" value="120" name="checkbox[]" /></td>
</tr>
</table>
<button type="submit">Update Database</button>
</form>
Then the PHP to process the request might look like this in basic form:
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
$userids=!empty( $_POST['userID'] ) ? $_POST['userID'] : false;
$fnames=!empty( $_POST['fname'] ) ? $_POST['fname'] : false;
$prices=!empty( $_POST['price'] ) ? $_POST['price'] : false;
$checkboxes=!empty( $_POST['checkbox'] ) ? $_POST['checkbox'] : false;
$sql='update `TABLE` set `fname`=?, `price`=?, `control`=? where `userID`=?';
$stmt=$db->prepare( $sql );
if( $stmt ){
$stmt->bind_param('siii', $fname, $price, $control, $uid );
foreach( $checkboxes as $index => $control ){
if( !empty( $control ) ){
$uid=$userids[ $index ];
$fname=$fnames[ $index ];
$price=$prices[ $index ];
$stmt->execute();
}
}
$stmt->close();
$db->close();
}
}
?>

submit input text on checked box

I want to submit a form which posts the input text field when the current line checkbox is checked.
When I submit, I get the productID of the checked box but all of the productLink. I want to only get the input text of the corresponding checked box.
How do I go about this?
<form action="process.php" method="post">
<table>
<tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[]" /></td>
</tr><tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[]" /></td>
</tr><tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[]" /></td>
</tr><tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[]" /></td>
</tr><tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[]" /></td>
</tr>
</table>
<input type="submit" name="formSubmit" value="Submit" />
</form>
You can find a lot of solution, so in my opinion you should look this one
HTML Element Array, name="something[]" or name="something"?
You can't this way, but there are some alternative method for example, when user check to checkbox then you could call a javascript function create an array and send json stringify data your php.
another way like below;
in your html
<form method="post">
<table>
<tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[<?= $products->id; ?>"]"></td>
</tr>
<tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[<?= $products->id; ?>"]"></td>
</tr>
<tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[<?= $products->id; ?>"]"></td>
</tr>
<tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[<?= $products->id; ?>"]"></td>
</tr>
<tr>
<td><input type="checkbox" name="productId[]" value="<?= $products->id; ?>" /> <input type="text" name="productLink[<?= $products->id; ?>"]"></td>
</tr>
</table>
<input type="submit" name="formSubmit" value="Submit" />
</form>
in your php (suppose in codeigniter because you tagged it)
function some_func_name(){
$product_ids = $this->input->post('productId');
$posted_product_links = $this->input->post('productLink');
$selected_links = [];
for($product_ids as $id){
$p_link = $posted_product_links[$id];
array_push($selected_links, array('id' => $id, 'link' => $p_link));
}
}
I hope this may help you..
update your by following codes
HTML
<form action="process.php" method="post">
<table>
<tr>
<td>
<input type="checkbox" name="productId[1]" value="<?=$products->id;?>" <?= set_checkbox('productId[1]', $products->id);?> />
<input type="text" name="productLink[1]" value="<?= set_value('productLink[1]');?>"/>
</td>
</tr><tr>
<td>
<input type="checkbox" name="productId[2]" value="<?=$products->id;?>" <?= set_checkbox('productId[2]', $products->id);?> />
<input type="text" name="productLink[2]" value="<?= set_value('productLink[2]');?>" />
</td>
</tr><tr>
<td>
<input type="checkbox" name="productId[3]" value="<?=$products->id;?>" <?= set_checkbox('productId[3]', $products->id);?>/>
<input type="text" name="productLink[3]" value="<?= set_value('productLink[3]');?>" />
</td>
</tr><tr>
<td>
<input type="checkbox" name="productId[4]" value="<?=$products->id;?>" <?= set_checkbox('productId[4]', $products->id);?> />
<input type="text" name="productLink[4]" value="<?= set_value('productLink[4]');?>" />
</td>
</tr><tr>
<td>
<input type="checkbox" name="productId[5]" value="<?=$products->id;?>" <?= set_checkbox('productId[5]', $products->id);?> />
<input type="text" name="productLink[5]" value="<?= set_value('productLink[5]');?>" />
</td>
</tr>
</table>
<input type="submit" name="formSubmit" value="Submit" />
</form>
Controller
public function some_func_name(){
if($this->input->post()){
$checkbox = $this->input->post('productId');
$textbox = $this->input->post('productLink');
$selected = #array_intersect_key($textbox, $checkbox);
echo '<pre>';
print_r($selected);
}
}
codes here : https://pastebin.com/UC1UFPRF
I ended up using jquery to remove the obj when they were empty.
$(document).ready(function(){
$("form").submit(function(){
$("input").each(function(index, obj){
if($(obj).val() == "") {
$(obj).remove();
}
});
});
});

Post table row of checkboxed column

I have a table like this
<form method="post" action="url">
<table>
<tr>
<td><input type="checkbox" name="cbox[]" value="1"></td>
<td><input type="text" name="name[]" value="first_name"></td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="2"></td>
<td><input type="text" name="name[]" value="second_name"></td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="3"></td>
<td><input type="text" name="name[]" value="third_name"></td>
</tr>
</table>
<input type="submit" value="send" />
</form>
then, when I post this form, I want to send only cbox[] and name[] of rows that have checkbox selected
Change your html to
<form id ='myform' method="post" action="url">
<input type='hidden' id='checkboxs' name='checkboxs'>
<input type='hidden' id='names' name='names'>
<table>
<tr>
<td><input type="checkbox" name="cbox[]" value="1"></td>
<td><input type="text" name="name1" value="first_name"></td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="2"></td>
<td><input type="text" name="name2" value="second_name"></td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="3"></td>
<td><input type="text" name="name3" value="third_name"></td>
</tr>
</table>
<input type="button" value="send" onclick="submitValue()"/>
</form>
and add function
function submitValue()
{
var checkboxs = [];
var names = [];
$(':checkbox:checked').each(function(i){
checkboxs[i] = $(this).val();
names[i] = $('input[name=name'+(i+1)+']').val();
});
console.log(checkboxs);
console.log(names);
$('#checkboxs').val(checkboxs);
$('#names').val(names);
$('#myform').submit();
}
your value in checkboxs and names
Hope this help

i need to insert array values into mysql php table, where separated with comma in a single row

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>

How to insert bulk data with same name field in php mysql

I had tried to insert bulk data with same name field contains multiple rows. But only single row is inserted.
How to insert bulk data as different values to insert into the database.
INSERT INTO table_name (username, luck_number, test, tester) VALUES (('$username', '$luck_number', '$test', '$tester').
<tr>
<td>1</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number" value="" />
</td>
<td>
<input type="text" name="big" value="" />
</td>
<td>
<input type="text" name="test" value="" />
</td>
<td>
<input type="text" name="tester" value="" />
</td>
</tr>
<tr>
<td>2</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number" value="" />
</td>
<td>
<input type="text" name="big" value="" />
</td>
<td>
<input type="text" name="test" value="" />
</td>
<td>
<input type="text" name="tester" value="" />
</td>
</tr>
<tr>
<td>3</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number" value="" />
</td>
<td>
<input type="text" name="big" value="" />
</td>
<td>
<input type="text" name="test" value="" />
</td>
<td>
<input type="text" name="tester" value="" />
</td>
</tr>
#nisha,In your scenario only single row is inserted because variables are same name so it's overridden, Please try below code, It will give you array of fields so you can easily create for-loop & do multiple insert with your query.
<form method="post" name="userdata">
<tr>
<td>1</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<tr>
<td>2</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<tr>
<td>3</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<input type="submit" name="submit">
</form>
**Note:**you can worry about the security issue letter. read the answer with the comment.
Store them in array by adding [ ] this in your input field
<tr>
<td>1</td>
<form action="" method="POST">
<input type="hidden" name="username" value="<?php echo $login_session; ?>"/>
<td><input type="text" name="luck_number[]" value=""/></td>
<td><input type="text" name="big[]" value=""/></td>
<td><input type="text" name="test[]" value=""/></td>
<td><input type="text" name="tester[]" value=""/></td>
</tr>
<tr>
<td>2</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>"/>
<td><input type="text" name="luck_number[]" value=""/></td>
<td><input type="text" name="big[]" value=""/></td>
<td><input type="text" name="test[]" value=""/></td>
<td><input type="text" name="tester[]" value=""/></td>
</tr>
<tr>
<td>3</td>
<input type="hidden" name="username" value="<?php echo $login_session; ?>"/>
<td><input type="text" name="luck_number[]" value=""/></td>
<td><input type="text" name="big[]" value=""/></td>
<td><input type="text" name="test[]" value=""/></td>
<td><input type="text" name="tester[]" value=""/></td>
</tr>
<tr></td><input type="submit" name="submit" value="submit"/><tr></td>
</form>
<?php
//connect with your database
for($i=0;$i<count($_POST['luck_number']);$i++)
{
//set the value for variable
$luck_number=$_POST['luck_number'][$i];
$test=$_POST['test'][$i];
$tester=$_POST['tester'][$i];
//run your query
//INSERT INTO table_name (username, luck_number, test, tester) VALUES (('$username', '$luck_number', '$test', '$tester').
}
First of all change all field names by adding [] at the end.
Second step, to parsing all values you may use something like this
for($i=0; $i < $count($_GET['username']); $i++)
{
$username = $_GET['username'][$i];
$luck_number= $_GET['luck_number'][$i];
$big= $_GET['big'][$i];
$test= $_GET['test'][$i];
$tester= $_GET['tester'][$i];
// insert into database
}
The reason of inserting single row instead of multiple rows is your input field name. You are using same name in different input field so when the server gets the reply it replace the duplicate name and the last occurrence is outputted.
The thing you have to do is to use array. If you use known number of rows then you can simply use a for loop to insert data.
<tr>
<td>1</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<tr>
<td>2</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<tr>
<td>3</td>
<input type="hidden" name="username[]" value="<?php echo $login_session; ?>" />
<td>
<input type="text" name="luck_number[]" value="" />
</td>
<td>
<input type="text" name="big[]" value="" />
</td>
<td>
<input type="text" name="test[]" value="" />
</td>
<td>
<input type="text" name="tester[]" value="" />
</td>
</tr>
<?php
for ($i=0; $i<count($_POST['username']); $i++)
{
mysql_query("INSERT INTO table_name (`username`, `luck_number`, `test`, `tester`) VALUES (('".$_POST['username'][$i]."', '".$_POST['luck_number'][$i]."', '".$_POST['test'][$i]."', '".$_POST['tester'][$i]."')");
}
?>
Note: Sanitizing variable is always been a good practice and strongly recommended.

Categories