douzui0143 2012-01-17 21:57
浏览 21
已采纳

将动态创建的表单字段传递给PHP

I am trying to pass dynamically created form fields to a php script, and it's causing me a huge headache.

I've done this in the past many times, and I can't figure out what i'm doing wrong here.

Below is an example of what's going on:

A brief explanation: I have a form with a textarea, there is a button named "Add More", when clicked, a new textarea is generated via javascript. The textareas values are pushed into an array named "comments". When I try to loop through this array within my php script, it only gives me the first item, and none of the dynamically created ones.

HTML

<form action="" method="post" enctype="multipart/form-data" accept-charset="utf-8">
<textarea name="comments[]"></textarea>
<a href="javascript: return null;" class="add-more">Add More</a>
</form>

JS

$(".add-more").click(function(){ 

       var new_field = '<textarea name="comments[]"></textarea>';
       $(this).before(new_field);

}); 

PHP - this is where the issue is, when I try to loop through the comments[] array, it only gives me the first one, and does not bring through any of the ones that were generated dynamically.

<?php

$comments = $_POST['comments'];

$commentString = "";
foreach($comments as $value) {
$commentString .= $value;
}

?>

So with the above, if I create 5 textareas using the "Add More" button, input some text into each one, and then submit the form, none of the dynamically created fields send through to the php.

Can anyone help?

Thanks!!

  • 写回答

4条回答 默认 最新

  • dqq22391 2012-01-18 16:31
    关注

    This has been resolved.

    The issue was being caused by an unclosed form above the one I was having the issues with.

    Thanks for the replies everyone!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单