Php 在POST数组中排列输入数据

Php 在POST数组中排列输入数据,php,jquery,arrays,forms,Php,Jquery,Arrays,Forms,我创建了一个由大量相似区域组成的表单。可以使用jQuery添加这些区域。每个区域等于用于装配产品的一个工作步骤,因此具有相同的输入字段(说明、持续时间等)。以下是代码(简化为两个硬编码区域): 因此我得到了期望的结果: Array ( [step] => Array ( [0] => Array ( [stepDescr] =>

我创建了一个由大量相似区域组成的表单。可以使用jQuery添加这些区域。每个区域等于用于装配产品的一个工作步骤,因此具有相同的输入字段(说明、持续时间等)。以下是代码(简化为两个硬编码区域):

因此我得到了期望的结果:

Array
(
    [step] => Array
        (
            [0] => Array
                (
                    [stepDescr] => 
                    [stepDur] => 
                )

            [1] => Array
                (
                    [stepDescr] => 
                    [stepDur] => 
                )

        )

)
如果我不使用这些数字,我的数组当然会变得不可用:

Array
(
    [step] => Array
        (
            [0] => 
            [1] => Array
                (
                    [stepDescr] => 
                )

            [2] => Array
                (
                    [stepDur] => 
                )

            [3] => Array
                (
                    [stepDescr] => 
                )

            [4] => Array
                (
                    [stepDur] => 
                )

        )

)

如何使用jQuery将这些数字(基于步数)添加到HTML代码(!)中?

尝试使用php使用循环:

<html>
<head>
</head>
<body>
<form action="insertNewProduct.php" method="post">
    <fieldset id="product-fieldset">
        <legend>Produkt</legend>
        <div id="step-container">
            <input type="hidden" name="step[]" />
            <?php for($i=0;$i<2;$i++) { ?>
            <div id="step-table" class="product-table">
                <table id="basis-table">
                    <!-- STEP-BASISDATEN -->
                    <thead id="step-basis">
                        <!-- Basisdaten Schritt -->
                        <tr>
                            <th class="data-title">Arbeitsschritt</th>
                        </tr>
                        <tr>
                            <th>Bezeichnung:</th>
                            <td><input type="text" name="step[<?php echo $i; ?>][stepDescr]"></td>
                        </tr>
                        <tr>
                            <th>Dauer:</th>
                            <td><input type="text" name="step[<?php echo $i; ?>][stepDur]"></td>
                        </tr>
                    </thead>
                </table>
            </div>
            <?php } ?>                
        </div>
    </fieldset>
    <input type="submit" id="sendForm" value="Send"><input type="reset" id="resetForm" value="Reset">
</form>
</body>
</html>

产品
阿尔贝茨切里特
贝泽希农:
你可以用。它们是HTML5规范的一部分

自定义数据属性用于存储页面或应用程序专用的自定义数据,没有更合适的属性或元素

输入字段的名称可以存储在自定义属性中:

<input type="text" name="step[0][stepDescr]" data-name="stepDescr" />

我想这应该能奏效

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
    var areaNbr = 0;
    function addArea(frm) {
        areaNbr ++;
        var area = '<p id="area'+areaNbr+'">Bezeichnung: <input type="text" name="step['+areaNbr+'][stepDescr]"> Dauer: <input type="text" name="step['+areaNbr+'][stepDur]"> <input type="button" value="Remove" onclick="removeArea('+areaNbr+');">';
        jQuery('#step-container').append(area);
    }

    function removeArea(areaNum) {
        jQuery('#area'+areaNum).remove();
    }
</script>
</head>
<body>
<form action="insertNewProduct.php" method="post">
    <fieldset id="product-fieldset">
        <legend>Produkt</legend>
        <div id="step-container">
            <input type="hidden" name="step[]" />
            <p id="area0">Bezeichnung: <input type="text" name="step[0][stepDescr]"> Dauer: <input type="text" name="step[0][stepDur]"> <input onclick="addArea(this.form);" type="button" value="Add Area" /> </p>
            </div>


        </div>
    </fieldset>
    <input type="submit" id="sendForm" value="Send"><input type="reset" id="resetForm" value="Reset">
</form>
</body>
</html>

var=0;
功能添加区域(frm){
areaNbr++;
变量区域=“

bezeichung:Dauer:”; jQuery(“#步骤容器”).append(区域); } 函数removeArea(区域数){ jQuery('#area'+areaNum).remove(); } 产品

bezeichung:Dauer:


您可以在php中使用
for()
foreach()
循环并打印此内容。我不想使用php重新排列和处理数组。我的目标是提交一个合适的数组。我认为-如果您有100个工作步骤或更多-一旦提交,正确更改数组顺序将变得非常复杂。请检查我的答案。也许这对你有帮助。你和上面的人有同样的想法。因此,我的答案是一样的。谢谢你,这帮了大忙。你能给我一些关于这个“数据名”的额外信息吗?确实也是一个有用的答案!非常感谢。
<input type="text" name="step[0][stepDescr]" data-name="stepDescr" />
function addStep(){
    var count = $('.step-container').length;
    var $container = $('.step-container:last').clone();
    $container.find(':input').each(function(){
        var input_name = 'step['+count+']['+$(this).attr("data-name")+']';
        $(this).attr('name', input_name);
    });
    $container.appendTo("#product-fieldset");
};
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
    var areaNbr = 0;
    function addArea(frm) {
        areaNbr ++;
        var area = '<p id="area'+areaNbr+'">Bezeichnung: <input type="text" name="step['+areaNbr+'][stepDescr]"> Dauer: <input type="text" name="step['+areaNbr+'][stepDur]"> <input type="button" value="Remove" onclick="removeArea('+areaNbr+');">';
        jQuery('#step-container').append(area);
    }

    function removeArea(areaNum) {
        jQuery('#area'+areaNum).remove();
    }
</script>
</head>
<body>
<form action="insertNewProduct.php" method="post">
    <fieldset id="product-fieldset">
        <legend>Produkt</legend>
        <div id="step-container">
            <input type="hidden" name="step[]" />
            <p id="area0">Bezeichnung: <input type="text" name="step[0][stepDescr]"> Dauer: <input type="text" name="step[0][stepDur]"> <input onclick="addArea(this.form);" type="button" value="Add Area" /> </p>
            </div>


        </div>
    </fieldset>
    <input type="submit" id="sendForm" value="Send"><input type="reset" id="resetForm" value="Reset">
</form>
</body>
</html>