【问题标题】:reading dynamically added input values into an array php将动态添加的输入值读入数组php
【发布时间】:2017-06-13 17:35:39
【问题描述】:

一切正常,期望在添加动态字段时,添加的输入不会被捕获到数组中。仅读取唯一创建的输入中的值。 HTML 部分

<table class="table table-bordered table-hover order-list" >
<thead>
    <tr><td>Product</td><td>Price (Ksh.) </td><td>Qty</td><td> (Ksh.)</td></tr>
</thead>

<tbody>
    <tr>
        <td><input type="text" class="form-control" name="product[]" required="" /></td>
        <td><input type="text" class="form-control" name="price[]" required/></td>
        <td><input type="text" class="form-control" name="quantity[]" /></td>
        <td><input type="text" name="linetotal[]" readonly="readonly" /></td>
        <td><a class="deleteRow"> x </a></td>
    </tr>
</tbody>

<tfoot>
    <tr>
        <td colspan="5" style="text-align: center;">
            <input type="button" id="addrow" value="Add Product" />
        </td>
    </tr>

    <tr>
        <td colspan="5">
            Grand Total: Ksh.<input type="text" name="grandtotal" readonly="readonly" /><span id="grandtotal"></span>
        </td>
    </tr>
    </tfoot>
    </table>

总结得到小计和总计的javascript如下:

$(document).ready(function () {
var counter = 1;

$("#addrow").on("click", function () {
    counter++;

    var newRow = $("<tr>");
    var cols = "";
    cols += '<td><input type="text" name="product' + counter + '"/></td>';
    cols += '<td><input type="text" name="price' + counter + '"/></td>';
    cols += '<td><input type="text" name="quantity' + counter + '"/></td>';
    cols += '<td><input type="text" name="linetotal' + counter + '" readonly="readonly"/></td>';
    cols += '<td><a class="deleteRow"> x </a></td>';
    newRow.append(cols);

    $("table.order-list").append(newRow);
});

$("table.order-list").on("change", 'input[name^="price"], input[name^="quantity"]', function (event) {
    calculateRow($(this).closest("tr"));
    calculateGrandTotal();
});

$("table.order-list").on("click", "a.deleteRow", function (event) {
    $(this).closest("tr").remove();
    calculateGrandTotal();
});
});
function calculateRow(row) {
var price = +row.find('input[name^="price"]').val();
var qty = +row.find('input[name^="quantity"]').val();


var linetotal = +row.find('input[name^="linetotal"]').val((price * qty).toFixed(2));
}
function calculateGrandTotal() {
 var grandTotal = 0;
 $("table.order-list").find('input[name^="linetotal"]').each(function () {
    grandTotal += +$(this).val();
});



$("#grandtotal").text(grandTotal.toFixed(2));
}

读取数组的php部分是

if(isset($_POST['cinvoice']) && $_SERVER["REQUEST_METHOD"] == "POST" &&is_array($_POST["product"]) && is_array($_POST["quantity"]) && is_array($_POST["price"]) && is_array($_POST["linetotal"]))

{

$recordid="";
$firstname="";
$product="";
$quantity="";
$price="";


$linetotal="";


foreach ($_POST["product"] as $key => $prod) {


 $product .= $prod.",";
}

foreach ($_POST["quantity"] as $key => $qty){ 



 $quantity.=$qty. ",";

}

   foreach ($_POST["price"] as $key => $prc) {



$price.=$prc. ",";

}

foreach ($_POST["linetotal"] as $key => $linetotal) {

  $linetotal.=$linetotal. ",";
}

【问题讨论】:

  • 您必须以数组格式声明名称。例如 ` name="product[' + counter + ']"'
  • 顺便说一句,您可以像这样为 3 个 foreach 保存行:$product = implode(',', $_POST["product"]) 等...
  • 是的。您在 htm 部分(即product[])中以数组格式声明名称,而在 javascript 中,您使用名称作为字符串(即product' +count + '
  • @FairyDancer 无需在名称中传递计数 :)

标签: javascript php html arrays


【解决方案1】:

您应该将文本框名称作为数组传递:

cols += '<td><input type="text" name="product[]"/></td>';
cols += '<td><input type="text" name="price[]"/></td>';
cols += '<td><input type="text" name="quantity[]"/></td>';
cols += '<td><input type="text" name="linetotal[]" readonly="readonly"/>

你也可以在php中使用implode函数

foreach ($_POST["product"] as $key => $prod) {
 $product .= $prod.",";
}

$product = implode(',', $_POST["product"])

【讨论】:

  • 谢谢。但是,将文本框作为数组传递仍然不会改变任何内容。
猜你喜欢
  • 2023-03-27
  • 1970-01-01
  • 2021-11-10
  • 2021-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
相关资源
最近更新 更多