PHP中是否可以动态创建新输入?

时间:2014-12-22 22:30:25

标签: php html forms

我希望用户能够提交多个输入/文本区域 - 但是他们可以创建的输入/区域数量没有限制。有没有办法在使用前一个表格的瞬间创建全新的表单条目?

e.g。

##########
#        #
#        #
##########

然后用户输入内容

##########    ##########
#  FOO   #    #        #
#        #    #        #
##########    ##########

弹出一个新的弹出窗口。我还想知道是否可以将新输入添加到php中的帖子,以及是否可以为每个新框分配一个新的输入名称,并考虑在php文件中创建新的输入名称。

这是否可以仅使用php,如果是这样,我将如何进行呢?

2 个答案:

答案 0 :(得分:0)

Javascript向页面添加新输入。创建变量并在创建输入时递增1,在删除时递减。输入的数组索引将基于此。

HTML

<input name="myinput[0]" type="text"  />
<input name="myinput[1]" type="text"  />

PHP。像这样的东西?

<?php
foreach ($_REQUEST['myinput'] as $item) {
    echo $item;

}
?>

答案 1 :(得分:0)

<script>
$(document).ready(function(){
   reload();
   function add_new(){
      $('#whatever').append('<input name="myinput[]" type="text" class="duces">');
   }
   function reload(){
      $('.duces').off();
      $('.duces').on('change',function(){
         add_new();
         //adds event handler on dynamically inserted elements
         reload();
      });
   }
});
</script>
<div id='whatever'>
   <input name='myinput[]' type='text' class='duces'>
</div>

确保你有jquery依赖。

基本上在提交表单后,元素作为数组传递,因此遍历数组并获取值。

foreach ($myinput as $val)
{
   echo $val;
}
相关问题