我对 jqGrid 有一些问题。
我添加了一个验证用户输入的方法。如果部门存在,则会显示警告消息,“部门存在!”
这是我的 jqGrid 代码:
afterSubmit: function(response, postdata){
var res = $.parseJSON(response.responseText);
if (res === "1") {
return [false,"Department Already Exist"];
} else {
return [true,"Sucess"];
}
}
还有我的 php add 方法:
if($oper == 'add') {
$deptid = $_POST['idms_department'];
$deptnm = $_POST['department'];
if(checkUser($deptnm) == "FALSE"){
return "1";
} else {
$ins = "INSERT INTO ms_department(department) VALUES('$deptnm')";
if(mysql_query($ins)){
"Success INSERT msDept";
} else {
die("Failed : " .mysql_error());
}
}
mysql_close();
} else .... (another operation)
消息本身没有显示。如何正确使用 afterSubmit 方法?谢谢
更新
我已经将方法更改为
crudMessage = function(response,postdata){
var res = response.responseText; // response text is returned from server.
if (res === "1") {
return [false,"Department Already Exist"];
} else {
return [true,"Sucess"];
}
}
然后从 jqgrid 正文中删除 afterSubmit 并将此行添加到 jqgrid 导航中:
jQuery("#departments").jqGrid('navGrid','#pager-departments',{edit:true,add:true,del:true}, {closeAfterEdit: true},{beforeShowForm: function(form) { $('#idms_department', form).hide(); },closeAfterAdd:true},{afterSubmit: crudMessage},{closeAfterSearch:true},{});
这是最新的 php 语法:
include 'configuration.php';
function checkDepartment($department){
$query = "SELECT department FROM ms_department WHERE department ='$department' LIMIT 1";
$result= mysql_query($query);
return mysql_num_rows($result);
}
if($oper == 'add') {
$deptid = $_POST['idms_department'];
$deptnm = $_POST['department'];
if(checkDepartment($deptnm) == 1){
echo '1';
} else {
$ins = "INSERT INTO ms_department(department) VALUES('$deptnm')";
if(mysql_query($ins)){
"Success INSERT msDept";
} else {
die("Failed : " . mysql_error());
}
}
mysql_close();
} else
if($oper == 'edit'){ ....