在javascript中动态添加文本框

I dynamically add a text box whenever a user clicks a button to add more, which works fine. However, in PHP when I get the submitted field values by $_POST['skills'] , I only receive the first text box value, not the dynamically added ones. I'm almost positive there is something wrong with the way I am adding the text boxes in javascript. I use the following method to add a text box field:

function addTextBoxField()
{
            var input = document.createElement('input'); 
            input.type = "text";
            input.name = "skills[]";
            input.size = "30";

            var container = document.getElementById("skillfield");
            container.appendChild(input);
}

The HTML code I have for the text box is :

...
<td>
<div id="skillfield">
    <input type="text" size="30" name="skills[]" />
</div>
</td>
<td><a href="javascript:void(0);" class="addLink hidden" onclick="addTextBoxField();"><div class="button">+ Add</div></a></td>

Here is the php code as well:

if ($_SERVER["REQUEST_METHOD"] == "POST")
{ 
     $allskills = $_POST['skills']; 
     $size = count($_POST['skills']);
     print_r($allskills);
}

The output is the following, even though I inputted three values

Array ( [0] => java )

douganmo1121
douganmo1121 php代码在哪里?它应该是'foreach($_POST['技能']作为$技能)echo$skill;`
5 年多之前 回复
dongya1875
dongya1875 在你的文字中你提到$_POST['技能']但你的var名称被声明为技能[],这是你问题中的拼写错误?
5 年多之前 回复
doufu1950
doufu1950 是<form>中的所有输入?
5 年多之前 回复

5个回答



你的意思是$ _POST ['技能 s </ strong>']的值

i不要认为 你的javascript有问题,你在php中如何阅读帖子数据有什么不对。 在这种情况下,帖子数据将是一个数组,所以你可以这样访问它

$ _ POST ['skills'] [0] //第一个输入的值</ code>
$ _POST ['技能'] [1] //第二次输入的值</ code> </ p>
</ div>

展开原文

原文

do you mean the values of $_POST['skills']
i dont think there is something wrong with your javascript, what is wrong is how you read the post data in your php. The post data will be an array in this case, so you access it like this
$_POST['skills'][0] //value of 1st input
$_POST['skills'][1] //value of 2nd input



您的字段名称是技能</ code>而不是技能</ code>。因此它应该是 $ _ POST ['技能'] </ code>。 $ _ POST ['skills'] </ code>在这种情况下是一个数组。 因此,访问尝试使用 var_dump($ _ POST ['技能']); </ code>,您将看到所有值。</ p>
</ div>

展开原文

原文

Your field name is skills not skill .So it should be $_POST['skills'].$_POST['skills'] is an array in this case. So access try with var_dump($_POST['skills']); and you will see all the values.



至于$ _POST ['skills']是一个数组,试试这个.. </ p>

< pre> $ skill = $ _POST ['skills'];

foreach($ skill as $ value){

echo $ value;

}
</ code> </ pre>

</ div>

展开原文

原文

As far as the $_POST['skills'] is an array, Try this..

$skill = $_POST['skills'];

foreach($skill as $value){

echo $value;

}

$_POST['skills'] is an array,So use print_r() to view the array.You may use as follows

<script type="text/javascript">
    function addTextBoxField()
    {
                var input = document.createElement('input'); 
                input.type = "text";
                input.name = "skills[]";
                input.size = "30";

                var container = document.getElementById("skillfield");
                container.appendChild(input);
    }
</script>
    <form method ="post">
        <td>
            <div id="skillfield">
                <input type="text" size="30" name="skills[]" />
            </div>
        </td>
        <td>
            <input type="submit" name="submit" value="Submit"/>
        </td>
    </form>
    <td><a href="javascript:void(0);" class="addLink hidden" onclick="addTextBoxField();"><div class="button">+ Add</div></a></td>
<?php 
    if(isset($_POST['submit']) && $_POST['submit'] == 'Submit'){
        echo "<pre>";
            print_r($_POST['skills']);
        echo "</pre>";
    }
?>
dongyisa6254
dongyisa6254 复制新文件中的所有代码并进行测试,它对我有用。添加多个文本框并填写表单。然后提交。
5 年多之前 回复
douying0108
douying0108 我尝试打印数组进行检查,它只显示为大小为1的数组,并在第一个文本框中输入值。
5 年多之前 回复

you could use the array length. Try to this

for($i=0;$i<=count($skills)-1;$i++)
{
  $per=$skills[$i]*$skills[$i];
  echo $per;
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐