简体   繁体   中英

Get values from table column if row checked

I am using PHP to get values from the table and need to process them further. If row is checked I need to get values from columns Quantity and FMK Code.

Table:

<table class="table table-bordered">
    <thead>
        <tr class="success">
            <th>#<br/></th> 
            <th>Article  </th>
            <th>Name  </th> 
            <th>Quantity</th> 
            <th>FMK CODE<br/></th>
        </tr>
    </thead>
    <tbody>
        <?php 
            $i = 1; 
            while($r=$q->fetch()){  ?>
            <tr>
                <td><input type="checkbox" class="from-control" name="id[]" value="<?php echo $r['id']?>"></td>
                <td><?=$r['Article']?></td>  
                <td><?=$r['Name']?></td>     
                <td><input type="text" class="form-control" value="<?=$r['quantity'];?>" name="quantity"></td>   
                <td> 
                    <select class="form-control col-lg-2" name="childCode"><?php getChildCodes($r["code"]) ?></select>
                </td>
            </tr>
        <?php } ?>      
    </tbody>
</table>

On submit I need to get values from "quantity and childCode" for each row selected.

<button type="submit" name="getValues"> Submit </button>

PhP processing:

<?php 

    if (isset($_POST['getValues'])) {

        $id = $_POST['id'];
        $quantity = $_POST['quantity'];
        $childCode = $_POST['childCode'];

        $values = array(); 

        foreach($id as $id) {
            foreach($quantity as $quant){
                foreach($childCode as $code){
                    array_push($values, $id,$quant,$code);
            }
        }           
        echo "<pre>";
        var_dump($values);
        echo "</pre>";          
    }
?>

values output. In output I get all values from table, no matter if they are checked or not which is wrong. Also array print was not well, values from one name form array. I need array to be formed first element of id, quantity, child code to b one array. [0]=>"177239","10.000",113

array(3) {
  [0]=>
  array(1) {
    [0]=>
    array(3) {
      [0]=>
      string(6) "177239"
      [1]=>
      string(6) "177240"
      [2]=>
      string(6) "177241"
    }
  }
  [1]=>
  array(3) {
    [0]=>
    string(6) "10.000"
    [1]=>
    string(7) "100.000"
    [2]=>
    string(6) "10.000"
  }
  [2]=>
  array(3) {
    [0]=>
    string(11) "113"
    [1]=>
    string(10) "87"
    [2]=>
    string(10) "91"
  }
}

misorude is pointing out that you need to name the form elements in a style that builds a PHP array. That is done with the [] characters as mentioned. So the first row would have element names like <input name="id[0]" ... , <input name="quantity[0]" ... . The second row would have element names like <input name="id[1]" and <input name="quantity[1]"

Also make sure the foreach block doesn't overwrite the $id variable :)

foreach($id as $id) // Oops!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM