显示来自服务器响应的错误消息 [英] Show Error Message from Server Response
问题描述
我在jqGrid上遇到一些问题.
I have some problems with jqGrid.
我添加了一种验证用户输入的方法.如果该部门存在,则会显示一条警告消息,部门已存在!"
I've added a method that validates users input. If the department exist then it will show a warning message, "Department is Exist!"
这是我的jqGrid代码:
Here's my jqGrid code:
afterSubmit: function(response, postdata){
var res = $.parseJSON(response.responseText);
if (res === "1") {
return [false,"Department Already Exist"];
} else {
return [true,"Sucess"];
}
}
我的php添加方法:
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方法? 谢谢
The message itself is not showing. How do you use the afterSubmit method properly? thanks
更新
我将方法更改为
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导航中:
then removing the afterSubmit from the jqgrid body and add this line into the jqgrid navigation:
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语法:
here's newest php syntax:
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'){ ....
推荐答案
在您的代码中,为什么要使用res.insertStatus
?从您提供的PHP代码片段中,您似乎想编写:
In your code, why are you expecting res.insertStatus
? From the snippet of PHP code that you provided, it seems you would want to write:
var res = $.parseJSON(response.responseText);
if (res === "User Already Exist!") {
或者也许我缺少了什么?您是否尝试过调试代码?
Or maybe I am missing something? Have you tried debugging your code?
那应该解释为什么alert
没有出现.
That should explain why the alert
is not appearing.
提交后 从服务器收到响应后触发.通常用于显示服务器的状态(例如,数据成功保存或出于服务器端编辑原因而取消保存).接收从请求返回的数据和id = value1,value2类型的已发布值的数组作为参数.
afterSubmit fires after response has been received from server. Typically used to display status from server (e.g., the data is successfully saved or the save cancelled for server-side editing reasons). Receives as parameters the data returned from the request and an array of the posted values of type id=value1,value2.
使用此事件时,应返回包含以下项目的数组[success, message, new_id]
其中
When used this event should return array with the following items [success, message, new_id]
where
成功是布尔值,如果为true,则继续处理;如果为false,则出现错误消息,并停止所有其他处理. (如果成功为真,则忽略消息).
success is a boolean value if true the process continues, if false a error message appear and all other processing is stopped. (message is ignored if success is true).
new_id可用于在网格中设置新的行ID. afterSubmit:函数(响应,后置数据) { … 返回[成功,消息,new_id] }
new_id can be used to set the new row id in the grid when we are in add mode. afterSubmit : function(response, postdata) { … return [success,message,new_id] }
根据这些文档,您的最终代码应删除alert
,而直接使用jqGrid:
Based on these docs, your final code should remove the alert
and just use jqGrid directly:
if (res === "User Already Exist!") {
return [false, "TODO: put your error message here"];
}
这篇关于显示来自服务器响应的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!