dongyou7739 2014-08-14 19:20
浏览 53
已采纳

使用Javascript向PHP脚本发送具有相同名称的表单字段时遇到问题

I have a form with with a simple input field and I have a button that will allow me to add another input field via javascript using the same name(eg: name="list[]"). When I click the 'add' button to enter multiple email addresses it dynamically adds the new input field but the problem I'm having is when I send it to the PHP script the script only returns the first e-mail and not the others. Any ideas what I could be doing wrong?

-javascript below-

function preparePage() {
  var addEmail = document.getElementById("add_email");
  var putEmail = document.getElementById("put_email");

  addEmail.onclick = function() {
    putEmail.innerHTML += '<input type="text" name="names_list[]"><br />';
    };
 }

window.onload =  function() {
  preparePage();
};

-html below-

  <form action="email_names.php" method="post">
    <div id="put_email">
      <input type="text" name="names_list[]"><br />
    </div>

    <div>
      <div id="add_email"> + </div>

      <input id="email_btn" type="submit" name="email_submit" value="E-mail List">
    </div>
  </form>

-php script below-

  if(isset($_POST['email_submit'])) {
    $names_list = $_POST['names_list'];

    echo '<pre>';
    print_r($names_list);
    echo '<pre>';
}

//This returns 
Array
(
  [0] => test@email1.com
)
// instead of
Array
(
  [0] => test@email1.com
  [1] => test@email2.com
  [2] => test@email3.com
)
  • 写回答

1条回答 默认 最新

  • duanli0453 2014-08-14 19:23
    关注

    Typos?

    putEmail.innerHTML += '<input type="text" name="name_list[]"   snip....
                                                    ^^^^-- singular name: no "s"
    
    
    
      <input type="text" name="names_list[]"><br />
                               ^^^^^---plural names. WITH "s"
    

    Doing a var_dump($_POST) would confirm this.


    and now I see the OP's done a ninja-edit and fixed this up, so guess not...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行