英文:
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:
<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>`
答案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['price1'] as $price) {
echo $price;
}
// Loop through the discount array and print each value
foreach ($_POST['discount'] as $discount) {
echo $discount;
}
Or use a function like print_r() like this:
// Print the price1 array in a readable format
print_r($_POST['price1']);
// Print the discount array in a readable format
print_r($_POST['discount']);
Or use a function like implode() like this:
// Convert the price1 array to a string separated by commas
$price1_string = implode(',', $_POST['price1']);
// Convert the discount array to a string separated by commas
$discount_string = implode(',', $_POST['discount']);
// Print the strings
echo $price1_string;
echo $discount_string;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论