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

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

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::

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

Here is the snippet of my HTML form:

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

PHP to get post values from the form

if(isset($_POST['submit'])) { // getting all values from the HTML form
    $price1 = $_POST['price1'];
    $discunt = $_POST['discunt'];
}

<table><tr><td>'.$_POST['price1'].'</td>
<td>'.$_POST['discunt'].'</td>
</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::

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

Here is the snippet of my HTML form:

&lt;form method=&quot;POST&quot; action=&quot;invoice.php&quot;&gt;
    &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;
    &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;
    &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;
    &lt;input type=&quot;submit&quot; name=&quot;submit&quot;&gt;
&lt;/form&gt;

PHP to get post values from the form

if(isset($_POST[&#39;submit&#39;])) { // getting all values from the HTML form
    $price1 = $_POST[&#39;price1&#39;];
    $discunt = $_POST[&#39;discunt&#39;];
}

&lt;table&gt;&lt;tr&gt;&lt;td&gt;&#39;.$_POST[&#39;price1&#39;].&#39;&lt;/td&gt;
&lt;td&gt;&#39;.$_POST[&#39;discunt&#39;].&#39;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;`

答案1

得分: 1

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

// 循环遍历price1数组并打印每个值
foreach ($_POST['price1'] as $price) {
    echo $price;
}

// 循环遍历discount数组并打印每个值
foreach ($_POST['discount'] as $discount) {
    echo $discount;
}

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

// 以可读格式打印price1数组
print_r($_POST['price1']);

// 以可读格式打印discount数组
print_r($_POST['discount']);

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

// 将price1数组转换为逗号分隔的字符串
$price1_string = implode(',', $_POST['price1']);

// 将discount数组转换为逗号分隔的字符串
$discount_string = implode(',', $_POST['discount']);

// 打印字符串
echo $price1_string;
echo $discount_string;
英文:

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

// Loop through the price1 array and print each value
foreach ($_POST[&#39;price1&#39;] as $price) {
    echo $price;
}

// Loop through the discount array and print each value
foreach ($_POST[&#39;discount&#39;] as $discount) {
    echo $discount;
}

Or use a function like print_r() like this:

// Print the price1 array in a readable format
print_r($_POST[&#39;price1&#39;]);

// Print the discount array in a readable format
print_r($_POST[&#39;discount&#39;]);

Or use a function like implode() like this:

// Convert the price1 array to a string separated by commas
$price1_string = implode(&#39;,&#39;, $_POST[&#39;price1&#39;]);

// Convert the discount array to a string separated by commas
$discount_string = implode(&#39;,&#39;, $_POST[&#39;discount&#39;]);

// Print the strings
echo $price1_string;
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:

确定