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...

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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?