PHP数组/表单帖子

问题描述 投票:0回答:1

我在此之前已经问过这个问题,但我不确定,问题是否足够清楚,或者是对的。所以现在我再问一遍,我的解决方案并非100%正确!

我有一张表格,我提交了它,里面有一张桌子。表格的每一行都有一个复选框,您可以检查订购产品,选择产品收费字段,当然还有价格(在选择字段中,所以我有一个价格值)。

实现的目标是在邮件中设置块,如下所示(下图):

product: greek salad xl
quantity: 4pcs.
price: 5.50$

product: tomatos
quantity: 8pcs.
price: 2.50$

....

所以我有这个,但价格总是取自第一个元素,所以,如果我选择第二个和第三个产品:价格取自第一个。数量取自第一个 - 它被移动一个。

我能做什么?

这是我尝试的(简单)foreach PHP设置(以及许多其他变体):

$product = $_POST['bestellung']; --> this is the checkbox that is  correct!
$quantity = $_POST['menge']; --> always shifted...
$price = $_POST['liq_preis']; --> always shifted...

foreach( $product as $key => $item ) {
$bestell_tab .= "\n\nProdukt: $item  \n\nStueckzahl: ".$quantity[$key]." \n\nPreis: ".$price[$key].";
}

($ bestell_tab我在PHP邮件中提交...)

如果有人可以通过“简单”解决方案帮助php-greenhorn,那就太棒了!

这里HTML标记:

<select class="kat-preis" name="liq_preis[]"><option value="10.00">10.00</option></select>
<select class="1-100" name="menge[]"><option value="1">1</option><option value="2">2</option></select>
<input type="checkbox" name="bestellung[]" value="expl-tomatos">

编辑

array(14) {
["tablepress-23_length"]=>
string(2) "10"
["menge"]=>
array(6) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "2"
[3]=>
string(1) "2"
[4]=>
string(1) "2"
[5]=>
string(1) "2"
}
["liq_preis"]=>
array(6) {
[0]=>
string(4) "1.00"
[1]=>
string(4) "2.00"
[2]=>
string(4) "3.00"
[3]=>
string(4) "4.00"
[4]=>
string(4) "5.00"
[5]=>
string(4) "6.00"
}
["bestellung"]=>
array(5) {
[0]=>
string(9) "tomatos109"
[1]=>
string(9) "tomatos111"
[2]=>
string(9) "tomatos116"
[3]=>
string(9) "tomatos118"
[4]=>
string(9) "tomatos209"
}

在此,我点击了第二个产品。第一个是空的,但价值不对。它仅适用于产品名称“bestellung”

编辑

<td class="column-1">51234</td><td class="column-2">goodchoice</td><td class="column-3">tomatos from Spain</td><td class="column-4"><select class="kat-preis" name="liq_preis[]"><option value="1.00">10.00</option></select></td><td class="column-5">1.00 CHF</td><td class="column-6"><select class="1-100" name="menge[]"><option value="1">1</option><option value="2">2</option></select></td><td class="column-7"><input type="checkbox" name="bestellung[]" value="51234"> </td>
php forms html-table html-email
1个回答
1
投票

php代码变为:

product = $_POST['bestellung']; --> this is the checkbox that is  correct!
$quantity = $_POST['menge']; --> allways shifted...
$price = $_POST['liq_preis']; --> allways shifted...

foreach( $product as $item ) {
$bestell_tab .= "\n\nProdukt: ".$item."  \n\n
Stueckzahl: ".$quantity[$item]." \n\n
Preis: ".$price[$item];//here there was an error in original code - an extra "   
}

要使上述工作正常,您必须将bestsellung的值放在另外两个数组中。用户怎么会知道西红柿呢?无论如何,html变成:

<table>
<td class="column-1">51234</td>
<td class="column-2">goodchoice</td>
<td class="column-3">tomatos from Spain</td>
<td class="column-4">
    <select class="kat-preis" name="liq_preis[51234]">
        <option value="1.00">10.00</option>
    </select>
</td>
<td class="column-5">1.00 CHF</td>
<td class="column-6">
    <select class="1-100" name="menge[51234]">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
</td>
<td class="column-7">
    <input type="checkbox" name="bestellung[]" value="51234"> 
</td>
</table>
© www.soinside.com 2019 - 2024. All rights reserved.