Post table row of checkboxed column - php

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

Related

Get All Data PHP-HTML (Including all Checkbox)

I have array of HTML data
<tr><input type="checkbox" name="checkbox[]" value="0"> <input type="text" name="char[]" value="A"></tr>
<tr><input type="checkbox" name="checkbox[]" value="1"> <input type="text" name="char[]" value="B"></tr>
<tr><input type="checkbox" name="checkbox[]" value="2"> <input type="text" name="char[]" value="C"></tr>
How can I get all of of item in PHP Post? I need to get all data and check if the checkbox were check or not.
Like so:
<?php
foreach($_POST["char"] as $data) {
//If Checkboxes Checked:
// DO INSERT CHECKED ROW
//Else If Not Checked:
// ECHO VALUE NOT CHECKED ROW
}
?>
If I'm trying, the data were different from what I has check or only the checked value that sent.
Thank you.
Use indexes in your field names:
<form method="POST">
<table>
<tr>
<td><input type="checkbox" name="checkbox[0]" value="0"></td>
<td><input type="text" name="char[0]" value="A"></td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox[1]" value="0"></td>
<td><input type="text" name="char[1]" value="B"></td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox[2]" value="0"></td>
<td><input type="text" name="char[2]" value="C"></td>
</tr>
</table>
<input type="submit" name="submit">
</form>
Then you can use a loop counter:
for ($i = 0; $i<count($_POST['char'];$i++) {
if (isset($_POST['checkbox'][$i]) {
// Do something if checkbox is set using $_POST['char'][$i]
} else {
// Do something if checkbox is not set
}
}

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

How to use associate arrays to get the id from the checkbox and appropreate value from the text field

Hi everyone, i'm new to programming. I need to develop a rating system with check boxes and text-fields where user clicks the subjects from the list and add his rating/experience in the text field. All these subjects and ratings are added to the database with the subject id and the rating. So the issue is, I don't know how to write the associate array to get the selected subjects and their appropriate rating to insert into the database. Can anyone please provide me some codes or samples similar to this. So, I can get some idea how to do this. Thanks in advance :)
This is the HTML sample code
<form action="" method="post">
<table width="372" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="24"><input type="checkbox" name="subid1" value="1" id="subid1"></td>
<td width="203">Maths</td>`enter code here`
<td width="145"><input type="text" name="sub1" id="sub1"></td>
</tr>
<tr>
<td><input type="checkbox" name="subid2" value="2" id="subid2" /></td>
<td>Physics</td>
<td><input type="text" name="subid2" id="subid2" /></td>
</tr>
<tr>
<td><input type="checkbox" name="subid3" value="3" id="subid3" /></td>
<td>Computing</td>
<td><input type="text" name="subid3" id="subid3" /></td>
</tr>
<tr>
<td><input type="checkbox" name="subid4" value="4" id="subid4" /></td>
<td>Chemistry</td>
<td><input type="text" name="subid4" id="subid4" /></td>
</tr>
<tr>
<td><input type="checkbox" name="subid5" value="5" id="subid5" /></td>
<td>Biology</td>
<td><input type="text" name="subid5" id="subid5" /></td>
</tr>
<tr>
<td><input type="checkbox" name="subid7" value="6" id="subid7" /></td>
<td>Human Biology</td>
<td><input type="text" name="subid6" id="subid6" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="button" id="button" value="Submit" /></td>
<td> </td>
</tr>
</table>
</form>
This will do the job on client side:
<input type="checkbox" name="cb[myID1]" value="1" />
<input type="text" name="t[myID1]" value="" />
<input type="checkbox" name="cb[myID2]" value="1" />
<input type="text" name="t[myID2]" value="" />
and this can be used on server side:
$usedTexts = array();
if ( array_key_exists("t", $_POST) && is_array($_POST["t"])
&& array_key_exists("cb", $_POST) && is_array($_POST["cb"])
)
{
$usedTexts = array_intersect_key($_POST["t"], $_POST["cb"]);
}
see manual for server side: http://us3.php.net/array_intersect_key
edit: fixed to POST; added array_key_exists() and is_array()

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