Php JQUERY提交表单并显示结果-页面上有多个表单
我刚接触Jquery,不久前正试图用类似的函数重新创建一些混乱的东西 我已将代码粘贴到此处: 我有一个表格,它是根据上一页表格的结果创建的。第一列中的学生姓名都可以使用,之后的每一列都是测试要求的结果,只是一个带有“通过/失败”的下拉列表,对于每个人和每个要求,我想继续并选择结果,并将结果提交至后台数据库,然后删除选择选项并显示结果 目前,在post_result.php上,我有这样一条消息,只需输入文本,我就会得到一个结果:Php JQUERY提交表单并显示结果-页面上有多个表单,php,jquery,forms,Php,Jquery,Forms,我刚接触Jquery,不久前正试图用类似的函数重新创建一些混乱的东西 我已将代码粘贴到此处: 我有一个表格,它是根据上一页表格的结果创建的。第一列中的学生姓名都可以使用,之后的每一列都是测试要求的结果,只是一个带有“通过/失败”的下拉列表,对于每个人和每个要求,我想继续并选择结果,并将结果提交至后台数据库,然后删除选择选项并显示结果 目前,在post_result.php上,我有这样一条消息,只需输入文本,我就会得到一个结果: if($_POST['studentID'] != ""){ ech
if($_POST['studentID'] != ""){
echo "Success";
}
非常感谢所有的帮助
编辑:取自粘贴库链接
<?php
ini_set('error_reporting', E_ALL);
$student_name = $_POST['student_name'];
$class = $_POST['class'];
$month = $_POST['month'];
?>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("select#result").change(function() {
var studentID = $(this).find("input[name='student_name']").val();
var result = $(this).serialize();
$.post('post_result.php', result, function(data) {
// We not pop the output inside the #output DIV.
$("#output-" + studentID).html(data);
});
$("#"+studentID+"-formarea").hide();
return false;
});
});
</script>
</head>
<body>
<?php
require 'db_connect.php';
$header=1;
$query = mysql_query("SELECT * FROM requirements WHERE `class` = '$class' && month = $month ") or die("Error:". mysql_errno());
?>
<table class="tg" border="1">
<tr>
<th class="tg-031e">Student Name</th>
<?php while ($row = mysql_fetch_array($query)) {
echo '<th class="tg-031e">'.$row['requirement'].'</th>';
$header++;
}
?>
</tr>
<?php
for ($i = 0; $i < count($student_name); $i++) {
echo '<tr>';
echo ' <td class="tg-031e">'.$student_name[$i].'</td>';
for ($count = 1; $count < $header; $count++) {
echo '<td><div id="'.$student_name.'-formarea">'
. '<form method="POST" id="form-'.$student_name.'">'
. '<input type="hidden" name="student_name" value="'.$student_name.'" />'
. '<select name="result" id="result"><option selected>Select Result</option>'
. '<option value="Pass">Pass</option><option value="Fail">Fail</option></select>'
. '</form></div>'
. '<div id="output-'.$student_name[$i].'"></div></td>';
}
echo'</tr>';
}
?>
</table>
</body>
</html>
您正在序列化一个Select元素,而不是表单,并且必须序列化表单。 例如:
<form id="myForm" action="blabla" method="blabla">
<select id="result" name="result">
<option>Bla Bla Bla</option>
</select>
<input type="text" name="student_name" />
<!-- ...more elements .... -->
</form>
在通过Ajax向其发送数据的文件中,您将“studentID”作为键,但在序列化的形式中,键是从name=属性分配的。这将使您的键为:“学生姓名”和“结果”
if($_POST['studentID'] != ""){
echo "Success";
}
确保这一点的一种方法是将以下内容放入Ajax发送和接收的php文件中。这将打印php文件接收的post内容数组
print_r($_POST);
该数组的结果将显示在div中,您将告诉Ajax放置结果
这是一种很好的方式来序列化您正在为Ajax工作的表单
var result = $(this).closest('form').serialize();
感谢您的快速回复-我已经更改了,但是仍然不起作用-什么都没有发生。感谢您的回复,我已经这样做了,但是仍然没有任何事情-如果我打开元素检查器并从下拉列表中选择一些内容,我可以看到它request post_result.php,但是没有回复?
var result = $(this).closest('form').serialize();