I have a checkbox as follows,
<tr>
<td>
<input type="checkbox" checked="checked" name="feegroupid[]" class="fee_checkbox" value="<?php echo $fee_value->fee_groups_feetype_id ?>">
</td>
<td>
<input type="text" name="actual_pay[]" class="form-control modal_amount actual_pay" id="amount" value="<?php echo $feetype_balance; ?>">
</td>
</tr>
And my PHP function if the checkbox row is selected,
foreach($_POST['feegroupid'] as $index => $selected) {
echo $index;
$actualPayment = $_POST['actual_pay'][$index];
}
I have two rows with checkbox,
Here the echo $index is displaying only the first row index 0 if i select the second row (whose index is 1).
But both is selected (ie.checked), it is showing both index as 0,1 respectively.. But when we uncheck one checkbox(say first row with index 0 is unchecked), it is showing the 0 only but checked index is 1.
I am in the need to get the value of another input box with name actual_pay[] based on the checked box index..
How to get the respective checked box index instead of the first row index while changing checkbox?
Attached image has a button click to pay which should send the checked box index (ie, second box is checked and its index is 1).
Something like this should work:
<tr>
<td>
<input type="checkbox" checked="checked" name="feegroupid[]" class="fee_checkbox" value="<?php echo $fee_value->fee_groups_feetype_id; ?>">
</td>
<td>
<input type="text" name="ap_<?php echo $fee_value->fee_groups_feetype_id; ?>" class="form-control modal_amount actual_pay" id="amount" value="<?php echo $feetype_balance; ?>">
</td>
</tr>
PHP:
if (isset($_POST['feegroupid'])) {
foreach ($_POST['feegroupid'] as $k => $v) {
if (isset($_POST['ap_' . $v])) {
echo $_POST['ap_' . $v]; //$actualPayment
} else {
echo 'Not found for item: ap_' . $v;
}
}
}
Related
**How to select checkbox in database mysqli
what should i write in checkbox value
please kindly help
i also implode function which convert array to string
did not find values for checkbox values to print. .....
Also check the output on PICTURE...
**
OUTPUT PIC
<?php
while($fetch=mysqli_fetch_array($select))
{
?>
<tr>
<td><?php echo $fetch["tableno"]?></td>
<td><?php echo $fetch["customerid"]?></td>
<td><?php echo $fetch["item"]?></td>
<td><?php echo $fetch["money"]?></td>
<td><button>Delete</button></td>
<td><a href="update.php?tableno=<?php echo $fetch["tableno"]; ?>"/><button>Update</button></td>
<td><form method="POST">
<input type="checkbox" name="chk[]" value="???">
<input type="submit" name="sub1" value="Button for Checkbox">
</td> </form>
</tr>
<?php
}?>
</table>
<p style="font-size:30px">The Customer has TO Selected these Items from Menu List
and therefore submitted to<br> Kitchener to Make the Food and
then waiter will serve the Food to Customer.</p>
</body>
</html>
<?php
if(isset($_POST['sub1']))
{
$chk=implode(',', $_POST['chk']);
echo "<br>Customer has selected the following checkbox Item to be Prepared and Eat the Food:<br/>";
echo "<br/>".$chk."<br><br/><br/><br/><br/>";
}
?>
Try this:
In you form, method to insert multiple checkboxes
<form method="POST" action="test2.php">
<input type="checkbox" name="chk[]" value="InputText1">InputText1<br>
<input type="checkbox" name="chk[]" value="InputText2">InputText2<br>
<input type="submit" name="sub1">
</form>
if(isset($_POST['sub1'])) // check the submitted form and print the values
{
echo "You selected the following checkbox:<br/>";
foreach ($_POST['chk'] as $value) {
echo $value."<br>";
}
One method is the value of a checkbox can be concatenated with all data and after submitting, check for selected check boxes with for each loop and print all the selected rows. Like below
<input type="checkbox" name="chk[]" value="<?php echo "<td>".$fetch["tableno"]."</td><td>".$fetch["customerid"]."</td><td>".$fetch["item"]."</td><td>".$fetch["money"]."</td>"; ?>">
And after posting use this :
if(isset($_POST['sub1'])) // check the submitted form and print the values
{
echo "Below Customers have placed orders:<br/>";
echo "<table>";
foreach ($_POST['chk'] as $value) {
echo "<tr>".$value."</tr>";
}
echo "</table>";
}
I am having a issue in getting the correct value from the table.
The issue I encountered:
When I choose the second checkbox the and input a quantity, it is Undefined offset: 0
But the value of the checkbox is working and correct.
What I am expecting is when I choose the second or other checkbox (exclude first checkbox), I should get the value of that input field.
HTML
<?php foreach($results as $row) { ?>
<tr>
<td><input type="checkbox" name="products[]" value="<?php echo $row->items_id; ?>"></td>
<td><?php echo $row->item_name; ?></td>
<td><input type="number" name="quantity[]" class="form-control"></td>
</tr>
<?php } ?>
<input type="submit" name="process" class="btn btn-primary" value="Process">
PHP
$quantity = $_POST['quantity'];
$products = $_POST['products'];
$count_selected = count($products);
for($i=0; $i< $count_selected; $i++){
var_dump($quantity[$i]); exit;
}
The problem with checkboxes is that an unchecked one submits no value so your $_POST['products'] and $_POST['quantity'] arrays may have different lengths.
I'd combine using a hidden input with specific array indexes.
For example
<?php foreach($results as $row) : ?>
<tr>
<td>
<input type="hidden" name="products[<?= $row->items_id ?>]" value="0">
<input type="checkbox" name="products[<?= $row->items_id ?>]" value="1">
</td>
<td><?= htmlspecialchars($row->item_name) ?></td>
<td><input type="number" name="quantity[<?= $row->items_id ?>]" class="form-control"></td>
</tr>
<?php endforeach ?>
Then the arrays will have the same indexes and you can iterate them with a foreach
foreach ($_POST['products'] as $itemId => $checked) {
// $checked represents the state of the checkbox
// you can access quantity via $_POST['quantity'][$itemId]
}
You could even create a nice filtered array of quantities via
$selections = $_POST['products'];
$quantities = array_filter($_POST['quantity', function($itemId) use ($selections) {
return $selections[$itemId];
}, ARRAY_FILTER_USE_KEY);
first, define array variable.
$products =array( );
$quantity=array( );
other wise every thing seems fine.
I'm working on a form that uploads an image to a server. The form contains checkboxes and select boxes formatted to a <table>. Each table row contains two columns. The First column iterates through a mysql query that displays a list of items (24 items to be exact) that is paired to a checkbox. The 2nd column iterates through a mysql query that displays list of clients.
What needs to be done are as follows:
When the form gets uploaded (via POST or GET) the checkboxes which only have check gets passed.
convert the subs_id (name parameter for the select box) to an array for easy processing for mysql insert.
Code Below. Thanks in advanced!
<form method="post" enctype="multipart/form-data" action="upload2.php">
Name: <input type="text" name="item_desc" value="">
Show Date: <input type="text" name="upload_date" class="date" size="10"> to <input type="text" name="expiry_date" class="date" size="10">
Item: <input type="file" name="item"> Display Length: <input type="number" value="8" name="show_time" size="3" style="width:50px"> secs
<table class="data-list" width="100%">
<tr>
<td>Widget</td>
<td>Client</td>
</tr>
<?php foreach($instance as $item):?>
<tr>
<td><input type="checkbox" id="checkAll"/> <?php echo $item['name']; ?></td>
<td><select id="selectBox" name="subs_id[]">
<option selected="selected" value="none">Select Client</option>
<?php foreach($subs as $client): ?>
<option value="<?php echo $item['id'] . '-' . $client['id']; ?>" client="<?php echo $client['client_name']; ?>" ><?php echo $client['client_name']; ?></option>
<?php endforeach ?>
</select>
</td>
</tr>
<?php endforeach; ?>
</table>
<br>
<input type="submit" value="upload">
</form>
<input type="checkbox" id="checkAll" name='client[]' value ="1" /> <?php echo $item['name']; ?>
name and value is missing
1 .Change your checkbox code to
<input type="checkbox" id="checkAll" name="checkId[]" value="<?php echo $item['name']; ?>"/> <?php echo $item['name']; ?>
Here, I am taking name as array checkId[] and value of checkbox value="<?php echo $item['name']; ?>"
This should work for you.
Your select box is fine. There is no need to change it. you can find value of select box by checking index.
I figured it out. What I did was detect when there was a change on the select box and assign the value of the select box to the check box's value.
$(".selectBox").change(function() {
$(this).parent().siblings().children('input').attr('value',$(this).val());
});
then, on the upload2.php, I used the code below to get the value of only the boxes with check
if(!empty($_POST['checkId'])) {
$filename = $this->unique_filename($this->extract_ext( $_FILES["item"]["name"]));
$folder = dirname(__FILE__) . '/items';
move_uploaded_file($_FILES['item']['tmp_name'],"{$folder}/{$filename}");
foreach($_POST['checkId'] as $check) {
//save to db
$split = explode("-",$check);
$subs_id = $this->use_table('clients')->where('client_name like "' . $split[2] . '%"')->where('site_id=' . $split[1])->fetch();
//$subs_id = $this->query('select * from clients where client_name like "Trinoma%" and site_id=1');
//print_r($subs_id[0][id]);
$data = array(
'item_name'=> 'widgets/' . strtolower(get_class($this)) . '/items/' . $filename,
'show_time'=> $_POST['show_time'],
'instance_id'=> $split[0],
'item_desc'=>$_POST['item_desc'],
'subs_id'=> $subs_id[0][id]
);
$this->use_table(TBL_NAME)->insert($data)->execute();
$mid = $this->last_insert_id();
$this->use_table(TBL_SCHED)->insert(array(
'multirotator_id'=>$mid,
'show_from'=>'00:00',
'show_to'=>'23:59',
'upload_date'=>$_POST['upload_date'],
'expiry_date'=>$_POST['expiry_date']
))->execute();
} //end of foreach
} //end of if(!empty($_POST['checkId']))
and voila! working code for me.
I would like to know how we can store the value selected in an auto populated drop down (HTML5) into a variable, all in PHP. I want to obtain this value BEFORE clicking the 'Submit' button.
<?php
$i=1;
$j=0;
$result=pg_exec($pgsql_conn,"select * from crm.product_info order by 1");
while ($row = pg_fetch_assoc($result))
{
?>
<td><input type="checkbox"
id= <?php
echo $row['product_id'];
?>
value= <?php
echo $i;
?>
name= "prod[]">
<?php echo $row['product_name']; ?>
</td>
<td>
<input type="button" value=<?php echo "$".$row['product_value']; ?> id="but">
</td>
<td>
Quantity
<select class="select" name="qty[]">
<?php echo "qty".$j; ?>
</select>
</td>
<td>
Amount
<input
id="amt"
type="text"
readonly
value=<?php
echo $row['product_value']
?> >
</td>
<?php
}
?>
I require the SELECTED value of 'quantity'.
This is how i auto populate the quantity. It is from 0-100.
$(document).ready(function() {
for(i=0;i<=100;i++)
{
$(".select").append("<option value=\""+i+"\">"+i+"</option>");
}
});
First of all, PHP is server side, so it can't handle changes made in the browser until a request is send to it. You will have to use javascript to know selected value in real time before submitting the form. You can manipulate this value using javascript. But if you really need to do some PHP with the selected value, your solution is to use Ajax for this.
I have a form with rows which are populated from a table. Each row has a "checkbox" which the user can check or not.
When the form is submitted I want to be able to read which checkbox have been selected and insert the result in to a data table.
My code so far
FORM:
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<table
<?php do { ?>
<tr>
<td>input type="text" name="InspectRoomNo" value="<?php print $row_InspectItems['AuditItemNo']; ?>"></td>
<td>php echo $row_InspectItems['AuditItem']; ?>td>
<td>input name="check[]" type="checkbox" ></td>
</tr>
<?php } while ($row_InspectItems = mysql_fetch_assoc($InspectItems)); ?>
<input type="submit" value="Insert record">
</table>
The insert: fetchs $Items from table
while($row = mysql_fetch_assoc($Items))
{
$array[] = $row['AuditItem'];
}
foreach($array as $id) {
$AuditItemID = mysql_real_escape_string($id);
if(isset($_POST['check'])){
$Checked = mysql_real_escape_string($_POST['check'][$row]);
}
}
The problem I am having is the returned values for all the checkbox is true, even if a checkbox was not selected.
Can anyone help me sort this issue.
Many thanks.
Do it like this:
if(!empty($_POST['check'])) {
foreach($_POST['check'] as $check) {
echo $check;
}
}
You should put the item id inside the checkbox name:
<td><input name="check[<?= $row_InspectItems['AuditItem']; ?>]" type="checkbox" /></td>
Then, you can simply iterate over it:
foreach ($_POST['check'] as $id => $value) {
// do stuff with your database
}
I'm assuming than whomever runs this script is trusted, because it would be easy to forge the list of ids; make sure the current user has permissions to update those records.
What is happening, is that only selected checkboxes get sent to the server, so you will see that your $_POST['check'] array (this is an array!) is smaller than the number of items you have displayed on the screen.
You should add your ID's so that you know what checkboxes got checked and adapt your php processing code to handle an array instead of a single value.
You are also overwriting your InspectRoomNo every row, so you should use an array there as well.
The form side would look something like:
<td><input type="text" name="InspectRoomNo[<?php echo row_InspectItems['AuditItemNo']; ?>]" value="<?php print row_InspectItems['AuditItemNo']; ?>"></td>
<td><?php echo $row_InspectItems['AuditItem']; ?></td>
<td><input name="check[<?php echo row_InspectItems['AuditItemNo']; ?>]" type="checkbox" ></td>