使用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
)

我有一个带有简单输入字段的表单,我有一个按钮,可以让我添加另一个输入字段 通过javascript使用相同的名称(例如:name =“list []”)。 当我单击“添加”按钮输入多个电子邮件地址时,它会动态添加新的输入字段,但我遇到的问题是当我将其发送到PHP脚本时,脚本只返回第一封电子邮件,而不是其他电子邮件。 任何想法我可能做错了吗? p>

-javascript下面 - p>

  function preparePage(){
 var addEmail = document。  getElementById(“add_email”); 
 var putEmail = document.getElementById(“put_email”); 
 
 addEmail.onclick = function(){
 putEmail.innerHTML + ='&lt; input type =“text”name  =“names_list []”&gt;&lt; br /&gt;'; 
}; 
} 
 
window.onload = function(){
 preparePage(); 
}; 
  code>   pre> 
 
 

下面的-html - p>

 &lt; form action =“email_names.php”method =“post”&gt; 
&lt;  div id =“put_email”&gt; 
&lt; input type =“text”name =“names_list []”&gt;&lt; br /&gt; 
&lt; / div&gt; 
 
&lt; div&gt; 
  &lt; div id =“add_email”&gt;  +&lt; / div&gt; 
 
&lt; input id =“email_btn”type =“submit”name =“email_submit”value =“电子邮件列表”&gt; 
&lt; / div&gt; 
&lt; / 表格&gt; 
  code>  pre> 
 
 

下面的-php脚本 - p>

  if(isset($ _ POST ['email_submit'])  ){
 $ names_list = $ _POST ['names_list']; 
 
 echo'&lt; pre&gt;'; 
 print_r($ names_list); 
 echo'&lt; pre&gt;'; 
} 
  
 //返回
Array 
(
 [0] =&gt; test@email1.com 
)
 //而不是
Array 
(
 [0] =&gt; test @ email1。  com 
 [1] =&gt; test@email2.com 
 [2] =&gt; test@email3.com 
)
  code>  pre> 
  div>

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