submit input text on checked box - php

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

Related

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

how to update multiple checkbox

I want to update the subject and other fields by adding multiple subjects, but the issue is it only saves one of subject which I have checked.
How can I solve this issue ?
Below is my code :
<?php
$status = "";
if(isset($_POST['new']) && $_POST['new']==1)
{
$host="localhost";//host name
$username="root"; //database username
$word="";//database word
$db_name="tuichk";//database name
$tbl_name="data"; //table name
$con=mysqli_connect("$host", "$username", "$word","$db_name")or die("cannot connect");//connection string
$id=$_REQUEST['id'];
$name =$_REQUEST['name'];
$stu_ic = $_REQUEST['stu_ic'];
$address = $_REQUEST['address'];
$contact = $_REQUEST['contact'];
$checkbox1=$_REQUEST['subject'];
$chk="";
$update="update data set name='".$name."', stu_ic='".$stu_ic."', address='".$address."', contact='".$contact."', sub='".$checkbox1."' where id='".$id."'";
mysql_query($update) or die(mysql_error());
$status = "Record Updated Successfully. </br></br><a href='view.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';
}else {
?>
this is my html form
<form name="form" method="post" action="">
<input type="hidden" name="new" value="1" />
<input name="id" type="hidden" value="<?php echo $row['id'];?>" />
<p><input type="text" name="name" placeholder="Enter Name" required value="<?php echo $row['name'];?>" /><input type="text" name="stu_ic" placeholder="Enter Student IC" required value="<?php echo $row['stu_ic'];?>" /></p>
<p><input type="text" name="address" placeholder="Enter Address" required value="<?php echo $row['address'];?>" /><input type="text" name="contact" placeholder="Enter Contact" required value="<?php echo $row['contact'];?>" /></p>
<div style="text-align:center">
<div style="width:400px;border-radius:6px;margin:0px auto">
<table border="1">
<tr>
<td colspan="2">Select Subject:</td>
</tr>
<tr>
<td>Bahasa Melayu</td>
<td><input type="checkbox" name="subject" value="Bahasa Melayu"></td>
</tr>
<tr>
<td>English</td>
<td><input type="checkbox" name="subject" value="English"></td>
</tr>
<tr>
<td>Mathematics</td>
<td><input type="checkbox" name="subject" value="Mathematics"></td>
</tr>
<tr>
<td>Science</td>
<td><input type="checkbox" name="subject" value="Science"></td>
</tr>
<tr>
<td>Sejarah</td>
<td><input type="checkbox" name="subject" value="Sejarah"></td>
</tr>
<tr>
<td>Geography</td>
<td><input type="checkbox" name="subject" value="Geography"></td>
</tr>
<tr>
<td>Additional Mathematics</td>
<td><input type="checkbox" name="subject" value="Additional Mathematics"></td>
</tr>
<tr>
<td>Chemistry</td>
<td><input type="checkbox" name="subject" value="Chemistry"></td>
</tr>
<tr>
<td>Physics</td>
<td><input type="checkbox" name="subject" value="Physics"></td>
</tr>
<tr>
<td>Biology</td>
<td><input type="checkbox" name="subject" value="Biology"></td>
</tr><tr>
<td>Principle Of Accounting</td>
<td><input type="checkbox" name="subject" value="Principle Of Accounting"></td>
</tr><tr>
<td>Ekonomi Asas</td>
<td><input type="checkbox" name="subject" value="Ekonomi Asas"></td>
</tr><tr>
<td>Perdagangan</td>
<td><input type="checkbox" name="subject" value="Perdagangan"></td>
</tr>
</table>
</div>
</form>
HTML input should be
<input type="checkbox" name="subject[]" value="Subject1">
<input type="checkbox" name="subject[]" value="Subject2">
In PHP have many options.
Option 1:
save subjects as string
$subjects = implode(',', $_POST['subject']);
retrieve as string and convert to array
$subjects = explode(',', $field);
Option 2: can save as JSON and retrieve as JSON and decode it.
this is my code inc html form:
<?php
$status = "";
if(isset($_POST['new']) && $_POST['new']==1)
{
$host="localhost";//host name
$username="root"; //database username
$word="";//database word
$db_name="tuichk";//database name
$tbl_name="data"; //table name
$con=mysqli_connect("$host", "$username", "$word","$db_name")or die("cannot connect");//connection string
$id=$_REQUEST['id'];
$name =$_REQUEST['name'];
$stu_ic = $_REQUEST['stu_ic'];
$address = $_REQUEST['address'];
$contact = $_REQUEST['contact'];
$checkbox1=$_REQUEST['subject'];
$subjects = implode(',', $_POST['subject']);
$subjects = explode(',', $field);
$chk="";
$update="update data set name='".$name."', stu_ic='".$stu_ic."', address='".$address."', contact='".$contact."', sub='".$checkbox1."' where id='".$id."'";
mysql_query($update) or die(mysql_error());
$status = "Record Updated Successfully. </br></br><a href='view.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';
}else {
?>
<div>
<form name="form" method="post" action="">
<input type="hidden" name="new" value="1" />
<input name="id" type="hidden" value="<?php echo $row['id'];?>" />
<p><input type="text" name="name" placeholder="Enter Name" required value="<?php echo $row['name'];?>" /><input type="text" name="stu_ic" placeholder="Enter Student IC" required value="<?php echo $row['stu_ic'];?>" /></p>
<p><input type="text" name="address" placeholder="Enter Address" required value="<?php echo $row['address'];?>" /><input type="text" name="contact" placeholder="Enter Contact" required value="<?php echo $row['contact'];?>" /></p>
<div style="text-align:center">
<div style="width:400px;border-radius:6px;margin:0px auto">
<table border="1">
<tr>
<td colspan="2">Select Subject:</td>
</tr>
<tr>
<td>Bahasa Melayu</td>
<td><input type="checkbox" name="subject[]" value="Bahasa Melayu"></td>
</tr>
<tr>
<td>English</td>
<td><input type="checkbox" name="subject[]" value="English"></td>
</tr>
<tr>
<td>Mathematics</td>
<td><input type="checkbox" name="subject[]" value="Mathematics"></td>
</tr>
<tr>
<td>Science</td>
<td><input type="checkbox" name="subject[]" value="Science"></td>
</tr>
<tr>
<td>Sejarah</td>
<td><input type="checkbox" name="subject[]" value="Sejarah"></td>
</tr>
<tr>
<td>Geography</td>
<td><input type="checkbox" name="subject[]" value="Geography"></td>
</tr>
<tr>
<td>Additional Mathematics</td>
<td><input type="checkbox" name="subject[]" value="Additional Mathematics"></td>
</tr>
<tr>
<td>Chemistry</td>
<td><input type="checkbox" name="subject[]" value="Chemistry"></td>
</tr>
<tr>
<td>Physics</td>
<td><input type="checkbox" name="subject[]" value="Physics"></td>
</tr>
<tr>
<td>Biology</td>
<td><input type="checkbox" name="subject[]" value="Biology"></td>
</tr><tr>
<td>Principle Of Accounting</td>
<td><input type="checkbox" name="subject[]" value="Principle Of Accounting"></td>
</tr><tr>
<td>Ekonomi Asas</td>
<td><input type="checkbox" name="subject[]" value="Ekonomi Asas"></td>
</tr><tr>
<td>Perdagangan</td>
<td><input type="checkbox" name="subject[]" value="Perdagangan"></td>
</tr>
</table>
</div>
</form>
<p><input name="submit" type="submit" value="Update" /></p>
</form>
<?php } ?>

ARRAY FORM DATA RECEIVED IN PHP

I want to received 4,5 row data in php. When I put values in the form like 3 or 4 row than I want to received the same data in php :(
I am trying to make a pos. When the customer order multiple product than the order row automatically increase but there is no limit 5, 10, 15 or else.
And I want to received the data value in same rows like
<form name="data" method="post" action="data_rec.php" enctype="multipart/form-data">
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
</tr>
<tr>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
</tr>
<tr>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
</tr>
<tr>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
</tr>
<tr>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
<td>
<input name="data[]" type="text" id="data1" />
</td>
</tr>
</table>
<input name="Submit" type="submit" value="SEND">
</form>
<?php
foreach (array_combine($_POST['data'], $_POST['data']) as $i => $data) {
$i."<br />";
echo $data."<br />";
echo "SAGOR"."<br />";
}
?>
Output :
1
SAGOR
2
SAGOR
3
SAGOR
4
SAGOR
5
SAGOR
6
SAGOR
7
SAGOR
8
SAGOR
SAGOR
But I need :
1 2 3 4
SAGOR
5 6 7 8
You can add rows to your form on the browser with JS or JQuery.
You must give each row's elements unique ids/names like: <input name="data2" type="text" id="data2" /> <input name="data3" type="text" id="data3" />...
You will receive them all in you $_POST when the user submit the form

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.

How to handle multiple same form fields shortway in 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>

Categories