0

I am beginner in php mysql and I have a problem below picture type of form how to save values of all rows or only selected row fields values:- name of fields are as :-

 foreach($data as $row){
 <tr>
 <td><input type="checkbox" value="$row['ProductID']" name="productID[]" /></td>
 <td> <input type="text" value="$row['Quantity']" name="quantity[]" /></td>
 <td><input type="text" value="$row['Price']" name="price[]" /></td>
 </tr>
}

enter image description here

1. Question is How to get only selected row fields value in php or jquery I have no Idea how to get only selected row fields value in php

asked Aug 9, 2015 at 2:37

1 Answer 1

1

If you are trying to get a listing of all your items with a note whether selected or not, just change your form names a bit (If I understand what you are looking for...):

<?php 
 $data[] = array('ProductID'=>123,"Quantity"=>1,"Price"=>"2.00");
 $data[] = array('ProductID'=>234,"Quantity"=>2,"Price"=>"1.50");
 $data[] = array('ProductID'=>345,"Quantity"=>1,"Price"=>"4.59");
 $data[] = array('ProductID'=>456,"Quantity"=>4,"Price"=>"1.99");
 foreach($data as $row){ ?>
 <tr>
 <td><input type="checkbox" name="product[<?php echo $row['ProductID'];?>][select]" /></td>
 <td><input type="text" value="<?php echo $row['Quantity']; ?>" name="product[<?php echo $row['ProductID'];?>][qty]" /></td>
 <td><input type="text" value="<?php echo $row['Price']; ?>" name="product[<?php echo $row['ProductID'];?>][price]" /></td>
 </tr>
<?php }

Gives you:

// Just loop through the [product] array looking for the 'select' = 'on' 
Array
(
 [product] => Array
 (
 [123] => Array
 (
 [qty] => 1
 [price] => 2.00
 )
 [234] => Array
 (
 [select] => on
 [qty] => 2
 [price] => 1.50
 )
 [345] => Array
 (
 [qty] => 1
 [price] => 4.59
 )
 [456] => Array
 (
 [qty] => 4
 [price] => 1.99
 )
 )
)

To get the selected items:

if(!empty($_POST['product'])) {
 foreach($_POST['product'] as $row) {
 if(!empty($row['select'])) {
 print_r($row);
 }
 }
}
answered Aug 9, 2015 at 5:01
Sign up to request clarification or add additional context in comments.

8 Comments

thanks for your response but can you please write code for how to loop through on select
Same way you do with the table: foreach($_POST['product'] as $row){...etc.
Thanking you again but pardon me I really do'not know how to collection only selected row fields value will u please write full code only for selected row it can be one or more ?
See my update. You should be able to do whatever you are looking to do with that.
if I do print_r($row[qty]) its shows undefined index because I want each field value of a particular row like checkbox value qty value price value ?
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.