我在阅读 POST 请求时遇到了问题。主要思想是当我按下一个按钮时,jQuery 会创建一个具有相同行但名称和 ID 已更改的新 div。
第一行示例:
<input type="text" name="pro_name_0" id="pro_name_0" placeholder="Produktname">
下一行
<input type="text" name="pro_name_1" id="pro_name_1" placeholder="Produktname">
等等。之后,数据必须在 POST 请求中发送到 PHP 文件。
PHP 文件将它们全部读取并放入数组中以供以后使用。
PHP代码:
<?php
require $_SERVER['DOCUMENT_ROOT'] . '/action/conn/dbc.php';
$fields = array();
$counter = $_POST['counter'];
if ($counter > 0) {
for ($i=0; $i <= $counter; $i++) {
$proName = mysqli_real_escape_string($conn, $_POST['pro_name_'.$i]);
echo $proName.'<b/r>';
$proAmount = mysqli_real_escape_string($conn, $_POST['pro_count_'.$i]);
echo $proAmount.'<b/r>';
$proPrice = mysqli_real_escape_string($conn, $_POST['pro_price_'.$i]);
echo $proPrice.'<b/r>';
}
}
问题是其中一些字段的值为 NULL,即使不是 NULL。我相信循环太快了。我正在寻求有关此问题的建议和解决方案。
的HTML:
<form action="run.php" method="post">
<input id="counter" name="counter" type="number" hidden value="0">
<table id="VerkaufTable">
<tr>
<th>Vers.: 0</th>
</tr>
<tr>
<td><input type="text" name="pro_name_0" id="pro_name_0" placeholder="Produktname"></td>
<td><input type="text" name="pro_count_0" id="pro_count_0" placeholder="Produktmenge"></td>
<td><input type="text" name="pro_price_0" id="pro_price_0" placeholder="Produktpreis"></td>
</tr>
</table>
<button name="submit">submit</button>
</form>
计数器对字段进行计数,因此 php 可以知道计数。
我想过用 jQuery 中的数据发送 POST 请求,所以我不必做这个数组。但是我对 jQuery 不是很好,所以现在它是我的首选方式。
向前看 :)
更新:这是我在var_dump($_POST)
array(11) {
["counter"]=> string(1) "2"
["pro_name_0"]=> string(5) "Ortel"
["pro_count_0"]=> string(1) "1"
["pro_price_0"]=> string(2) "20"
// Second line
["pro_name_1_"]=> string(7) "Samsung"
["pro_count_1"]=> string(1) "2"
["pro_price_1"]=> string(2) "10"
// Third line
["pro_name_2_"]=> string(10) "Headphones"
["pro_count_2"]=> string(1) "1"
["pro_price_2"]=> string(2) "20"
["submit"]=> string(0) ""
}
// This is from the ones inside the loop
Notice: Undefined index: pro_name_1 in E:\Hosted\testField\run.php on line 10
Notice: Undefined index: pro_name_2 in E:\Hosted\testField\run.php on line 10