如何通过POST方法传递并显示动态创建的输入字段中的值?

huangapple go评论119阅读模式
英文:

How can I pass and display values from dynamically created input fields via POST method?

问题

I have a form in my HTML page which contains input fields that are dynamically generated. I am able to successfully store the data entered into these fields in a MySQL database. However, my goal is to pass and display the data after the user clicks on the submit button. When I attempt to do this, I get the following error::

  1. Notice: Array to string conversion in C:\xampp\htdocs\abc\Invoice.php on line 112 Array

Here is the snippet of my HTML form:

  1. <form method="POST" action="invoice.php">
  2. <td><input type="text" class="price form-control text-end" name="price1[]"></td>
  3. <td><input type="text" class="discount form-control text-end" name="discount[]"></td>
  4. <td><input type="text" class="qty form-control text-end" name="qty[]"></td>
  5. <input type="submit" name="submit">
  6. </form>

PHP to get post values from the form

  1. if(isset($_POST['submit'])) { // getting all values from the HTML form
  2. $price1 = $_POST['price1'];
  3. $discunt = $_POST['discunt'];
  4. }
  5. <table><tr><td>'.$_POST['price1'].'</td>
  6. <td>'.$_POST['discunt'].'</td>
  7. </tr></table>
英文:

I have a form in my HTML page which contains input fields that are dynamically generated. I am able to successfully store the data entered into these fields in a MySQL database. However, my goal is to pass and display the data after the user clicks on the submit button. When I attempt to do this, I get the following error::

  1. Notice: Array to string conversion in C:\xampp\htdocs\abc\Invoice.php on line 112 Array

Here is the snippet of my HTML form:

  1. &lt;form method=&quot;POST&quot; action=&quot;invoice.php&quot;&gt;
  2. &lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;price form-control text-end&quot; name=&quot;price1[]&quot;&gt;&lt;/td&gt;
  3. &lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;discount form-control text-end&quot; name=&quot;discount[]&quot;&gt;&lt;/td&gt;
  4. &lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;qty form-control text-end&quot; name=&quot;qty[]&quot;&gt;&lt;/td&gt;
  5. &lt;input type=&quot;submit&quot; name=&quot;submit&quot;&gt;
  6. &lt;/form&gt;

PHP to get post values from the form

  1. if(isset($_POST[&#39;submit&#39;])) { // getting all values from the HTML form
  2. $price1 = $_POST[&#39;price1&#39;];
  3. $discunt = $_POST[&#39;discunt&#39;];
  4. }
  5. &lt;table&gt;&lt;tr&gt;&lt;td&gt;&#39;.$_POST[&#39;price1&#39;].&#39;&lt;/td&gt;
  6. &lt;td&gt;&#39;.$_POST[&#39;discunt&#39;].&#39;&lt;/td&gt;
  7. &lt;/tr&gt;&lt;/table&gt;`

答案1

得分: 1

为了打印数组,您可以像这样使用foreach循环:

  1. // 循环遍历price1数组并打印每个值
  2. foreach ($_POST['price1'] as $price) {
  3. echo $price;
  4. }
  5. // 循环遍历discount数组并打印每个值
  6. foreach ($_POST['discount'] as $discount) {
  7. echo $discount;
  8. }

或者使用像print_r()这样的函数:

  1. // 以可读格式打印price1数组
  2. print_r($_POST['price1']);
  3. // 以可读格式打印discount数组
  4. print_r($_POST['discount']);

或者使用像implode()这样的函数:

  1. // 将price1数组转换为逗号分隔的字符串
  2. $price1_string = implode(',', $_POST['price1']);
  3. // 将discount数组转换为逗号分隔的字符串
  4. $discount_string = implode(',', $_POST['discount']);
  5. // 打印字符串
  6. echo $price1_string;
  7. echo $discount_string;
英文:

To print arrays, you can use a foreach loop like this:

  1. // Loop through the price1 array and print each value
  2. foreach ($_POST[&#39;price1&#39;] as $price) {
  3. echo $price;
  4. }
  5. // Loop through the discount array and print each value
  6. foreach ($_POST[&#39;discount&#39;] as $discount) {
  7. echo $discount;
  8. }

Or use a function like print_r() like this:

  1. // Print the price1 array in a readable format
  2. print_r($_POST[&#39;price1&#39;]);
  3. // Print the discount array in a readable format
  4. print_r($_POST[&#39;discount&#39;]);

Or use a function like implode() like this:

  1. // Convert the price1 array to a string separated by commas
  2. $price1_string = implode(&#39;,&#39;, $_POST[&#39;price1&#39;]);
  3. // Convert the discount array to a string separated by commas
  4. $discount_string = implode(&#39;,&#39;, $_POST[&#39;discount&#39;]);
  5. // Print the strings
  6. echo $price1_string;
  7. echo $discount_string;

huangapple
  • 本文由 发表于 2023年7月4日 21:46:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定