我有以下的HTML代码的形式:PHP提交与使用jQuery。员额()多个提交按钮
<form method="post" action="the_file.php" id="the-form">
<input type="text" name="the_input" value="<?php if (isset($_POST['the_input']) echo $_POST['the_input]; ?>">
<button type="submit" name="eat_something" value="TRUE">Eating</button>
<button type="submit" name="eat_something" value="FALSE">Don't Eat</button>
</form>
<textarea id="result"></textarea>
跟此JS:
$('#the-form').bind('submit', submitForm);
function submitForm(evt) {
jQuery.post(
$(this).attr('action'),
$(this).serialize(),
function(data) {
$('#result').empty().append(data).slideDown();
});
evt.preventDefault();
}
我也有一个PHP脚本在提交时从输入中接收$ _POST值并运行条件来测试哪个提交按钮被点击。
像这样:
$input = $_POST['the_input'];
$eating = $_POST['eat_something'];
if ($eating == 'TRUE') {
// Do some eating...
} else {
// Don't you dare...
}
如果我不使用jQuery.post()函数从按钮提交值被公布。但是,出于某种原因,我无法通过jQuery.post()函数将按钮值传递给PHP $ _POST。如果我不使用jQuery.post(),则输出不会附加到textarea,而是以单独页面上的文本格式(如文档)附加。我也试着在按钮$('button[type=submit]').bind('submit', submitForm);
上调用提交功能,但是这也不能解决我的问题。
在此先感谢您的帮助。
'。员额()'是一个Ajax调用。您可以定义提交的值。如果您想提交按钮值,请将它们一起发送。 – 2013-03-08 03:52:20
不知道如何完成,建议? – thinkus 2013-03-08 03:54:34
我给了一个答案...没有测试,但它应该是接近。 – 2013-03-08 04:15:01