请帮助我如何使用jQuery提交多个记录以便使用php进行更新。以下是html代码:
string += "<tbody><tr class='rowca'><td>#</td><td>" +value.sID+
"</td><td>" +value['Full Names']+ "</td><td><input type='text' name='ca1[]'
id='ca1' class='entry' maxlength='2' value='" +value.ca1+ "'></td><td><input
type='text' name='ca2[]' id='ca2' class='entry' maxlength='2' value='"
+value.ca2+ "'></td><td><input type='text' name='ca3[]' id='ca3'
class='entry' maxlength='2' value='" +value.ca3+ "'></td><td><input
type='text' name='ca4[]' id='ca4' class='entry' maxlength='2' value='"
+value.exam+ "'></td><td><input type='text' name='ca5[]' id='ca5'
class='total' readonly value='" +value.total+ "'></td><td><input type='text'
name='ca6[]' id='ca6' class='demo' readonly value='" +value.grade+ "'></td>
<td><input type='text' name='ca7[]' id='ca7' class='remark' readonly
value='" +value.remarks+ "'></td></tr><input type='text' name='sid[]'
id='sid' value='" +value.sID+ "'>";
});
string += "</tbody></table>";
string += "<p><input type='button' value='Save CAs' id='savecas'></p>";
使用以下代码只会更新第一行学生记录而不是多条记录:
$(function(){
$('#savecas').on('click',function(){
var url = 'update_student_cas_ajax.php';
var ca_1 = $('#ca1').val();
var ca_2 = $('#ca2').val();
var ca_3 = $('#ca3').val();
var ca_4 = $('#ca4').val();
var ca_5 = $('#ca5').val();
var ca_6 = $('#ca6').val();
var ca_7 = $('#ca7').val();
var sids = $('#sid').val();
var sess = $('#session').val();
var trm = $('#term').val();
var subj = $('#subject').val();
var cid = $('#staffclass').val();
if(confirm('Are You Sure You want to submit all CAs'))
{
if(!sess || !trm || !subj || !sids || !ca_1 || !ca_2 || !ca_3 || !ca_4 || !ca_5 || !ca_6 || !ca_7 || !cid)
{
$('.error').show(100).html('All Fields Are Required. Thanks').delay(500).fadeOut(2000);
}else{
$.post(url, {term:trm, subj:subj, sid:sids, ca1:ca_1, ca2:ca_2, ca3:ca_3,
ca4:ca_4, ca5:ca_5, ca6:ca_6, ca7:ca_7, sess:sess, cid:cid}).done(function(data){
if(data>0){
$('.success').show(100).html('CAs Saved Successfully').delay(1000).fadeOut(2000);}else{
$('.error').show(100).html('Unable to save Students CAs').delay(2000).fadeOut(1000);
}
});
}
}
});
});
我刚刚添加了名为update_student_cas_ajax.php的PHP代码
$session = trim($_POST['sess']);
$cID = trim($_POST['cid']);
$subID = trim($_POST['subj']);
$term = trim($_POST['term']);
$ca1 = trim($_POST['ca1']);
$ca2 = trim($_POST['ca2']);
$ca3 = trim($_POST['ca3']);
$exam = trim($_POST['ca4']);
$total = trim($_POST['ca5']);
$grade = trim($_POST['ca6']);
$remark = trim($_POST['ca7']);
$sID = trim($_POST['sid']);
$stmt = $db->prepare("UPDATE terminal_assessment SET ca1=:ca_1, ca2=:ca_2,
ca3=:ca_3, exam=:ca_4, total=:ca_5, grade=:ca_6, remarks=:ca_7 WHERE
term=:trm AND session=:sess AND classID=:class_ID AND subID=:sub_ID AND
sID=:stID");
$data = $stmt->execute(array(":ca_1"=>$ca1, ":ca_2"=>$ca2, ":ca_3"=>$ca3,
":ca_4"=>$exam, "ca_5"=>$total, ":ca_6"=>$grade, ":ca_7"=>$remark,
":class_ID"=>$cID, ":trm"=>$term, ":sub_ID"=>$subID, ":sess"=>$session,
":stID"=>$sID));
if($data)
{
$result = 1;
}
echo $result;
$db = null;
?>