0

我尝试在网上搜索解决此问题的方法,但找不到任何解决方案。问题是 - 我有一个表单和一个带有 javascript 函数的按钮,它添加了额外的输入。将这些输入变量传递给 php$_POST数组后,它会打印出除动态创建的输入之外的所有内容。你们知道,为什么动态生成的输入不显示吗?

<form action="page.php" method="post" id="rekins">
   <p>Invoice Number: <input type="text" name="nr" /></p>
   <p>Account: <input type="text" name="konts" /></p>
   <h1>Services:</h1>
   <h2>1. Service</h2>
   <input type="text" name="pak[0][counter]" style="display:none" value="1"/>
   <p>Name<input name="pak[0][name]" type="text" id="pakName0" class="pak"/></p>
   <p>Count<input name="pak[0][count]" type="text" id="pakCount0" class="pak"/></p>
   <p>Price<input name="pak[0][Price]" type="text" id="pakPrice0" class="pak"/></p>
   <input type="button" onclick="addInput()" name="add" value="Add another service" />
   <p><input type="submit" name="submit" value="Submit" /></p>
</form>
<script language="javascript">
    fields = 0;
    function addInput() {
    fields += 1;
    document.getElementById('rekins').innerHTML += 
            "<h2>"+(fields+1)+". service</h2>"+
            "<input type='text' name='pak["+fields+"][counter]' style='display:none' value='"+(fields+1)+"'/>"+
            "<p>Name<input name='pak["+fields+"][name]' type='text' id='pakName"+fields+"' class='pak'/></p>"+
            "<p>Count<input name='pak["+fields+"][count]' type='text' id='pakCount"+fields+"' class='pak'/></p>"+
            "<p>Price<input name='pak["+fields+"][Price]' type='text' id='pakPrice"+fields+"' class='pak'/></p>";
    }
</script>

我尝试调试var_dump($_POST['pak']);并显示默认值,但没有显示动态添加的值。

可能是什么问题?

4

1 回答 1

0

我试过了,它对我有用。

html端:

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <form action="x.php" method="POST" id="rekins">
       <p>Invoice Number: <input type="text" name="nr" /></p>
       <p>Account: <input type="text" name="konts" /></p>
       <h1>Services:</h1>
       <h2>1. Service</h2>
       <input type="text" name="pak[0][counter]" style="display:none" value="1"/>
       <p>Name<input name="pak[0][name]" type="text" id="pakName0" class="pak"/></p>
       <p>Count<input name="pak[0][count]" type="text" id="pakCount0" class="pak"/></p>
       <p>Price<input name="pak[0][Price]" type="text" id="pakPrice0" class="pak"/></p>
       <input type="button" onclick="addInput()" name="add" value="Add another service" />
       <p><input type="submit" name="submit" value="Submit" /></p>
    </form>
    <script language="javascript">
        fields = 0;
        function addInput() {
        fields += 1;
        document.getElementById('rekins').innerHTML += 
                "<h2>"+(fields+1)+". service</h2>"+
                "<input type='text' name='pak["+fields+"][counter]' style='display:none' value='"+(fields+1)+"'/>"+
                "<p>Name<input name='pak["+fields+"][name]' type='text' id='pakName"+fields+"' class='pak'/></p>"+
                "<p>Count<input name='pak["+fields+"][count]' type='text' id='pakCount"+fields+"' class='pak'/></p>"+
                "<p>Price<input name='pak["+fields+"][Price]' type='text' id='pakPrice"+fields+"' class='pak'/></p>";
        }
    </script>
    </body>
    </html>

php端:

<?php
 print_r( $_POST['pak'] );
?>

结果 :

Array ( [0] => Array ( [counter] => 1 [name] => [count] => [Price] => ) [1] => Array ( [counter] => 2 [name] => asd [count] => asd [Price] => asd ) )
于 2013-09-30T21:13:39.013 回答