gpt4 book ai didi

php - 数组不适用于动态表单字段

转载 作者:行者123 更新时间:2023-11-29 08:39:56 25 4
gpt4 key购买 nike

我已经找到了此表单所需的大部分内容(使字段动态化等),但是现在其中的数组部分似乎无法正确提交。

我想要实现的目标:具有选择字段的表单,可以动态复制该字段,然后将其作为表单的一部分提交到其自己的表中。因此,如果我们在一个表单中添加并选择三个人,它会提交到它自己的参加表,并使用外键返回该表单所针对的事件。必须使其充满活力,因为我们永远无法确定有多少人会参加上述事件,但它必须以一种形式发生。只是因为它确实如此。我老板也是这么说的。

这是我的添加另一个字段按钮的 JavaScript:

    $(document).ready(function() {
$('#btnAdd').click(function() {
var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
var newNum = new Number(num + 1); // the numeric ID of the new input field being added

// create the new element via clone(), and manipulate it's ID using newNum value
var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

// manipulate the id value of the input inside the new element
newElem.children(':first').attr('id', 'attendee' + newNum).attr('name', 'attendee[' + newNum + ']');

// insert the new element after the last "duplicatable" input field
$('#input' + num).after(newElem);

// enable the "remove" button
$('#btnDel').attr('disabled','');

// business rule: you can only add 5 names
if (newNum == 6)
$('#btnAdd').attr('disabled','disabled');
});

该字段的开头格式如下:

    <div id="input1" style="margin-bottom:4px;" class="clonedInput">
<select name="attendee[1]" id="attendee1" style='float:right;margin-right:4.5%;'>
<option value=''>Please choose one...</option>
<?php
while($row_attendees = mysql_fetch_assoc($res_attendees)){
$attendee_id = $row_attendees['attendee_id'];
$attendee_name = $row_attendees['name'];

echo "<option value='".$attendee_id."'>".$attendee_name." </option>";
}
?>
</select><label style='width:100px;display:inline-block;line-height:28px;' for="attendee">Attendee</label>
</div>

我正在正确地更改所有内容。所有选择的输入都被正确地标识和命名。 div 也进行了同样的更新。所有这些都工作正常。不是什么时候我去提交。这是我的 php:

    foreach($_POST['attendee'] as $attendee){
$sql_attendees = "INSERT into marketing_calendar.attending (event_title, attendee_id) VALUES ('".$_POST['title']."','".$attendee."')";
$res_attendees = mysql_query($sql_attendees) or die(mysql_error());
}

我用来将其整合在一起的所有教程都表明这是正确的。然而它不起作用。我只得到第一个下拉列表的内容,并且没有其他内容填充到数组中。如果我运行表单或在 foreach 语句中回显与会者变量,至少这就是它显示/提交的全部内容。请帮忙! :)

提前致谢。

更新我已经尝试了与其他用户讨论的几种方法来显示 $_POST['attendee'] 的数组,但是它仍然只显示数组中的 1 个 id,而不是我实际添加的许多字段。我还尝试从选择的名称属性中的数组中删除数字。所以它只是 name='attendee[]' 而不是 name='attendee[1]' 等等。这也没有任何帮助。有人可以帮忙解释一下为什么我的动态添加的字段没有添加到数组中吗?

最佳答案

我把你的代码放入JSfiddle中,在这里:http://jsfiddle.net/rv8Mv/1/

看起来选择已正确添加。您可以通过单击“提交”按钮进行检查,该按钮显示将提交到服务器的数据字符串。

您可能需要检查的一件事是确保将所有选择元素都包含在 <form> 中。元素,您没有将其包含在问题中。

我认为您的问题出在服务器上的 PHP 代码中。

在服务器上,确保您使用以下代码接收所有变量:

<?php
foreach($_POST as $key => $value){
error_log($key.' -> '.$value;
}
?>

然后检查错误日志以查看所有 POST 变量的名称和值。

您可能没有在当前的 PHP 代码中正确引用 POST 变量。

关于php - 数组不适用于动态表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14124534/

25 4 0
文章推荐: php - 将数据从 MySQL 输出到多行 HTML,在 textarea 中有效,但在
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com