0

我在阅读 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
4

1 回答 1

0

您根本不需要计数器,因为您可以重新格式化 FORM 以使用分组输入,如下所示:

<form action="run.php" method="post">
    <table id="VerkaufTable">
        <tr>
            <th>Vers.: 0</th>
        </tr>
        <tr>
            <td><input type="text" name="pro[0][name]"  id="pro_name_0"  placeholder="Produktname"></td>
            <td><input type="text" name="pro[0][count]" id="pro_count_0" placeholder="Produktmenge"></td>
            <td><input type="text" name="pro[0][price]" id="pro_price_0" placeholder="Produktpreis"></td>
        </tr>
    </table>
    <button name="submit">submit</button>
</form>

并且您可以在添加字段时调整 JS 以将其添加到 id01

            <td><input type="text" name="pro[1][name]"  id="pro_name_0"  placeholder="Produktname"></td>
            <td><input type="text" name="pro[1][count]" id="pro_count_0" placeholder="Produktmenge"></td>
            <td><input type="text" name="pro[1][price]" id="pro_price_0" placeholder="Produktpreis"></td>

这将输出 POST 如下:

pro = [
    [
        'name' => 'Ortel',
        'count' => '1',
        'price' => '20'
    ],
    [
        'name' => 'Samsung',
        'count' => '2',
        'price' => '10'
    ],
    [
        'name' => 'Headphones',
        'count' => '1',
        'price' => '20'
    ]
]

并且您将能够$_POST更轻松地处理输出

编辑

并且由于您询问了 Mysql,它应该与您发布的内容类似,但有一些差异:

if (!empty($pro)) {
  foreach ($pro as $p) {
      $name = mysqli_real_escape_string($conn, $p['name']);
      $count = mysqli_real_escape_string($conn, $p['count']);
      $price = mysqli_real_escape_string($conn, $p['price']);

      // do your mysql query here...

  }
}
于 2020-07-05T01:17:12.133 回答