复选框以选择行表的项目并获取用户在from中输入的金额

时间:2015-08-02 15:45:35

标签: php jquery mysqli

我想使用一个复选框来选择行表的项目,但是我有问题要获取用户在此处输入的每个项目的数量是我的html代码。我想用php有人可以帮帮我吗?

<form id="RegisterUserForm" action="register.php" method="post">

                    <table id="t01">
                        <tr>
                            <th width="5%">Select</th>
                            <th width="20%">Picture</th>
                            <th width="55%">Description</th>
                            <th width="10%">Amount</th>
                            <th width="10%">Price</th>
                        </tr>
                        <tr> 
                            <td><input id="pedido1" name="check_list[]" type="checkbox" value="1"/>Product 1</td>
                            <td id="imagem1"><img src="imagen1.png"></td>
                            <td>Descriptiondo Product 1</td>
                            <td><input id="qt" name="qt" type="text" class="text" value="1" /></td>
                            <td>13.50</td>
                        </tr>
                        <tr> 
                            <td><input id="pedido1" name="check_list[]" type="checkbox" value="1"/>Product 2</td>
                            <td id="imagem1"><img src="imagen2.png"></td>
                            <td>Descriptiondo Product 2</td>
                            <td><input id="qt" name="qt" type="text" class="text" value="1" /></td>
                            <td>13.50</td>
                        </tr>
                        <tr> 
                            <td><input id="pedido1" name="check_list[]" type="checkbox" value="1"/>Product 3</td>
                            <td id="imagem1"><img src="imagen3.png"></td>
                            <td>Descriptiondo Product 3</td>
                            <td><input id="qt" name="qt" type="text" class="text" value="1" /></td>
                            <td>13.50</td>
                        </tr>
                        <tr> 
                            <td><input id="pedido1" name="check_list[]" type="checkbox" value="1"/>Product 4</td>
                            <td id="imagem1"><img src="imagen4.png"></td>
                            <td>Descriptiondo Product 4</td>
                            <td><input id="qt" name="qt" type="text" class="text" value="1" /></td>
                            <td>13.50</td>
                        </tr>
                    </table>

                        <button id="register" type="submit">register</button>
                </form>

1 个答案:

答案 0 :(得分:0)

只是一个想法,但为什么你有一个复选框和数量相同的形式?我的意思是,如果所有金额都以0开头(或空白)并且用户更改为另一个数字,则表明他想要该产品。

然后,您只能在表单中使用此参数(每个参数都具有特定名称,可能包含产品的ID)。类似的东西:

<form>
    //rest of html
    <input name="qt[idproduct]" type="text" class="text" value="" />
    //rest of html
    <input name="qt[idproduct]" type="text" class="text" value="" />
    //rest of html
    <input name="qt[idproduct]" type="text" class="text" value="" />
    //rest of html
</form>

此表单的输入将生成一个数组参数qt,您可以通过以下方式获取值:

<?php
    if(isset($_GET['qt'])) { //or $_POST
        foreach ($_GET['qt'] as $id => $total) {
            if(!empty($total) && $total > 0 && is_numeric($total)) { // just checking if you are receiving a number bigger than 0
                echo "Product: " . $id . " total: " . $total . "<br />"; // do your thing
            }
        }
    }
?>

<强>更新

checkboxes,形式:

<form>
    //rest of html

    <input name="check_list[idproduct1]" type="checkbox" value="1"/>Product 1
    <input name="qt[idproduct1]" type="text" class="text" value="" />
    //rest of html

    <input name="check_list[idproduct2]" type="checkbox" value="1"/>Product 2
    <input name="qt[idproduct2]" type="text" class="text" value="" />

    //rest of html

    <input name="check_list[idproduct3]" type="checkbox" value="1"/>Product 3
    <input name="qt[idproduct3]" type="text" class="text" value="" />

    //rest of html
    <input type="submit">
</form>

PHP:

<?php
    if(isset($_GET['check_list'])) { //or $_POST
        foreach ($_GET['check_list'] as $id => $value) {
            $total = $_GET['qt'][$id];
            if(!empty($total) && $total > 0 && is_numeric($total)) { // just checking if you are receiving a number bigger than 0
                echo "Product: " . $id . " total: " . $total . "<br />"; // do your thing
            }
        }
    }
?>