2015-02-19 12:09
采纳率: 0%
浏览 29


You can usually use something like this:

<form action="example.php" method="POST">
<input type="text" name="file_name[]">
<input type="text" name="file_name[]">
<input type="text" name="file_name[]">

to collect data under the same name, and access it in PHP like:

$file_name = $_POST["file_name"];
echo $file_name[0]; //first occurrence
echo $file_name[2]; //third occurrence

but when it comes to generating dynamic fields with jQuery, like this:

$("#example_table").append("<tr><td><input type="text" name="file_name[]"></td></tr>");

and submitting it with standard submit button within POST form, the outcome differs. Only the last occurrence gets passed but the array indexes represent consecutive letters of such, not exact, indicated field as above.

The question is, why and how to fix it?

图片转代码服务由CSDN问答提供 功能建议





  $ file_name = $ _POST [“ file_name”];
 echo $ file_name [0];  //第一次出现
 echo $ file_name [2];  //第三次



  $(“#example_table”)。append(“     ”);


并使用POST表单中的标准“提交”按钮进行提交,结果有所不同。 仅最后一次出现通过,但数组索引表示上述表示字段的连续字母,但并不完全。


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • weixin_33671935
    weixin_33671935 2015-02-19 12:13

    You can use single quote instead of double quote for file_name. Might be this is useful to you.

    $("#example_table").append("<tr><td><input type='text' name='file_name[]'></td></tr>");
    点赞 评论
  • weixin_33704234
    weixin_33704234 2015-02-19 12:21

    You have to remove another double quotes from the html string. You have two ways for that

    1. $("#example_table").append("<tr><td><input type=\"text\" name=\"file_name[]\"></td></tr>");
    2. $("#example_table").append('<tr><td><input type="text" name="file_name[]"></td></tr>');

    Maybe this will help.

    点赞 评论
  • weixin_33709364
    weixin_33709364 2015-02-19 12:25

    So, the problem is solved. It was all about using double quotes instead of single ones. Thank you for your participation and support.

    点赞 评论