如何将动态生成的表单存储不同的字段值返回数据库php mysql?

时间:2015-08-09 02:37:33

标签: php jquery mysql

我是php mysql的初学者,我有一个问题,如下图片类型的表单如何保存所有行的值或只选择行字段值: - 字段名称如下: -

  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。问题是如何在php或jquery 中仅获取选定的行字段值 我不知道如何只在php中选择行字段值

1 个答案:

答案 0 :(得分:1)

如果您试图获取所有项目的列表,无论是否选择了注释,只需更改您的表单名称(如果我理解您要查找的内容......):

<?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 }

给你:

// 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
                )
        )
)

获取所选项目:

if(!empty($_POST['product'])) {
    foreach($_POST['product'] as $row) {
            if(!empty($row['select'])) {
                    print_r($row);
                }
        }
}