通过 AJAX 添加的表单字段无法加载到 $_POST 数组中
我有一个简单明了的 HTML 表单,允许人们订购一些小册子。表单首先加载看起来有点像这样的内容:
<script type="text/javascript">
var tableRowN = 1;
</script>
<form id="Order" name="Order" method="post" action="includes/orderCheck.php">
<input id="name" type="text" name="name" width="100" />
<table id="orderingTable">
<tr class="lastRow">
<td><div id="itemGroupdiv1">
<input type="text" class="disabled" name="itemGroup1" id="itemGroup1" />
</div></td>
<td><div id="itemCodediv1">
<input type="text" name="itemCode1" id="itemCode1" class="disabled" />
</div></td>
<td><div id="itemCodeVersiondiv1">
<input type="text" class="disabledSmall" id="itemcodeversion1" name="itemcodeversion1" />
</div></td>
</tr>
</table>
<input type="submit" name="submit" id="submit"/>
</form>
然后,当用户想要向表中添加新行时,他可以单击一个按钮,该按钮会触发以下 javascript 函数,以通过 AJAX 获取新表代码并将其插入。
function createItemLine() {
tableRowN++;
$('tr.lastRow').attr('class', '');
$('#orderingTable').append('<tr class="lastRow"></tr>');
$.ajax({
url: "/orderingTable.php?rNumber=" + tableRowN,
cache: false,
success: function(html){
$("tr.lastRow").append(html);
alert('loaded');
}
});
}
然后,AJAX 函数运行到 PHP 脚本,该脚本创建下一行,滚动 ID 和名称等,并在数字上加上 +1。
<td><div id="itemGroupdiv2">
<input type="text" class="disabled" name="itemGroup2" id="itemGroup2" />
</div></td>
<td><div id="itemCodediv2">
<input type="text" name="itemCode2" id="itemCode2" class="disabled" />
</div></td>
<td><div id="itemCodeVersiondiv2">
<input type="text" class="disabledSmall" id="itemcodeversion2" name="itemcodeversion2" />
</div></td>
到目前为止,没有什么令人惊讶的吗?一切都应该非常简单......
问题是,当我添加新行(在 Firefox 和 Chrome 中)时,新行完全被表单提交过程忽略,并且它们永远不会传递到 $_POST 数组中。
这是一个已知问题吗?我以前没有遇到过这个...
感谢您的指点, H
I've got a plain and simple HTML form which allows people to order some brochures. The form first loads with something looking a little like this:
<script type="text/javascript">
var tableRowN = 1;
</script>
<form id="Order" name="Order" method="post" action="includes/orderCheck.php">
<input id="name" type="text" name="name" width="100" />
<table id="orderingTable">
<tr class="lastRow">
<td><div id="itemGroupdiv1">
<input type="text" class="disabled" name="itemGroup1" id="itemGroup1" />
</div></td>
<td><div id="itemCodediv1">
<input type="text" name="itemCode1" id="itemCode1" class="disabled" />
</div></td>
<td><div id="itemCodeVersiondiv1">
<input type="text" class="disabledSmall" id="itemcodeversion1" name="itemcodeversion1" />
</div></td>
</tr>
</table>
<input type="submit" name="submit" id="submit"/>
</form>
Then when the user wants to add a new line to the table he can click a button which fires the following javascript function to grab the new table code via AJAX and insert it.
function createItemLine() {
tableRowN++;
$('tr.lastRow').attr('class', '');
$('#orderingTable').append('<tr class="lastRow"></tr>');
$.ajax({
url: "/orderingTable.php?rNumber=" + tableRowN,
cache: false,
success: function(html){
$("tr.lastRow").append(html);
alert('loaded');
}
});
}
The AJAX function then runs off to a PHP script which creates the next line, rolling the IDs and Names etc with +1 to the number.
<td><div id="itemGroupdiv2">
<input type="text" class="disabled" name="itemGroup2" id="itemGroup2" />
</div></td>
<td><div id="itemCodediv2">
<input type="text" name="itemCode2" id="itemCode2" class="disabled" />
</div></td>
<td><div id="itemCodeVersiondiv2">
<input type="text" class="disabledSmall" id="itemcodeversion2" name="itemcodeversion2" />
</div></td>
So so far, nothing suprising? Should all be pretty straight forward...
The problem is that when I add new lines (In Firefox and Chrome) the new lines are completely ignored by the form submission process, and they never get passed through into the $_POST array.
Is this a known problem? I've not come across this before...
Thanks for any pointers,
H
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 jQuery.trim(data) 但这不太确定,因为会影响
您的数据内容。或者查看这个可能会帮助你
use
jQuery.trim(data)
but this is not pretty sure because can affect thecontent of your data. or see this one may help u
你的表缺少 html id 吗? jQuery 选择器 $('#orderingTable') 正在寻找 id="orderingTable" 的内容
Is your table missing an html id? The jQuery selector $('#orderingTable') is looking for something with id="orderingTable"
在一些彻底的(我的意思是彻底的)事实证明,以下简单(但很明显)的 HTML 错误可能会导致此问题: 格式
在创建经过正确验证的 HTML 时,表单已提交,所有值均已正确传递到 _POST 数组中。这是一个实例课,可确保您的开发人员在尝试使用他们的编码方法之前注意基础知识;)
On some thorough (and boy do I mean thorough) it turned out that the following simple (yet obvious) HTML errors can cause this issue:
On creating properly validated HTML, the form submitted and all values were passed correctly into the _POST array. An object lesson in making sure your developers pay attention to the basics before trying to get all fancy in their coding approach ;)
我发现使用
.html()
插入内容而不是.append()
或.append()
jquery.com/prepend" rel="nofollow">.prepend()
使插入的表单字段按预期工作。I've found that using
.html()
to insert the content instead of.append()
or.prepend()
causes the inserted form fields to work as expected.我刚刚花了很长时间来解决这样的问题。
我正在使用 ajax 将输入字段插入表单,但该输入字段没有显示在 $_POST 提交数组中,这真是太烦人了!!!!无论如何,我通过检查所有 html 来修复它,结果发现我的表单“打开”位于页面上的主 div 之一内部,而不是外部。
因此:
现在固定为:
愚蠢,我知道,但在巨大的形式中,很难发现!简而言之,只要整理好你的 html,它就会起作用,我希望能对某个地方的人有所帮助:-)
M
I've just spent quite a while laboring over a problem like this.
I was ajax-ing an input field into a form and that input field was not showing up in the $_POST submission array, was completely annoying!!!! Aaaaanyway, I fixed it by just checking over all my html and it turns out that my form 'open' was inside one of the main div's on page and not outside.
thus:
is now fixed to be:
Silly, I know, but in a massive form, it was tricky to spot! So in short just be tidy with your html and it WILL work, I hope that helps someone somewhere :-)
M