我正在构建一个网页,用户可以在其中“签署”某些协议并查看签名和新的(未签名)协议。为此,网页使用下面代码中的2个函数从数据库中获取相关信息,然后构建网页。不幸的是,当 - 并且仅当 - php文件(process.php)返回变量nrofagreements = 0时,控制台显示get函数的错误;对于任何其他值nrofagreements> 0没有错误发生 - 请参阅下面的控制台日志。我还检查了(使用Netbeans调试器)process.php正常工作 - 请参阅下面的json输出nrofagreements = 0-但我仍然怀疑错误必须在这个php文件中(?)任何想法出了什么问题以及如何解决它?
A1。 jquery代码:
$(document).ready(function(){
function NewAgreements(){
var jqxhr = $.get( "process.php", //get all
{ command: "getnewagreements", val1: "", val2: "" },
function(msg) {
},
"json"
).done(function(msg) {
console.log( "NewAgreement. $.get result: second success" );
//some code here to build the form based on msg from $.get()
form = buildNewAgreementsform(msg); //build form with supplier agreements and related products
$("#wrapper").append(form);
})//.done(function()
.fail(function() {
console.log( "NewAgreement. $.get result: error" );
})//.fail(function()
.always(function() {
console.log( "NewAgreement. $.get result: finished" );
});//$.get(
}//function getX()
//used by: tabs-agreements
NewAgreements();
function SignedAgreements(){
var jqxhr = $.get( "process.php", //get all
{ command: "getsignedagreements", val1: "", val2: "" },
function(msg) {
//var ecorp_eprodselectid;
},
"json"
).done(function(msg) {
console.log( "SignedAgreement. $.get result: second success" );
//some code to build the form based on the msg info from $.get()
form = buildSignedAgreementsform(msg); //build form with supplier agreements and related products
$("#wrapper-signedagreements").append(form);
})//.done(function()
.fail(function() {
console.log( "SignedAgreement. $.get result: error" );
})//.fail(function()
.always(function() {
console.log( "SignedAgreement. $.get result: finished" );
});//$.get( ",
}
SignedAgreements();
}); //$(document).ready
A2简化的PHP代码:
session_start();
$response_array = array();
$cmd = $_GET['command'];
switch($cmd){ //command handler
case 'getsignedagreements':
//some code here
$response_array['nrofagreements'] = $k;
if($response_array['nrofagreements'] == 0){
$response_array['status_general'] = 'success';
break;
}
//some code here
break;
case 'getsignedagreements':
//some code here
break;
default: //invalid command from webpage
$response_array['status_general'] = 'error';
break;//default
}//switch
$str = json_encode($response_array);
echo json_encode($response_array);
B中。日志控制台:
NewAgreement. $.get result: error
NewAgreement. $.get result: finished
SignedAgreement. $.get result: second success
SignedAgreement. $.get result: finished
函数NewAgreements的C log json输出:
"{"nrofagreements":0,"status_general":"success"}"
答案 0 :(得分:0)
我没有针对你的具体方法,但我有一些能帮助你指明正确方向的东西......
系统设计
我们所珍视的一个理念是,所有事情都需要以最有效的方式做自己的工作。大多数开发人员面临的问题是,他们对应用程序的每个部分将会做什么感到困惑,因此尝试&用一个组件做5个工作
我之所以解释这个问题是因为您的部分问题是否应该使用JS或PHP进行验证。答案是你必须知道系统的哪个部分会做 - 看起来你正在使用PHP来验证输入是否对会话有效,所以我建议你只处理那个部分的逻辑。 PHP文件
<强>的Ajax 强>
就个人而言,我会在你的$.get
命令中处理响应,但我会改为Ajax作为我自己的偏好:
$.ajax({
url: "process.php",
data: { command: "getnewagreements", val1: "", val2: "" },
success: function (msg) {
//handle validation in here
},
error: function(msg) {
//only fires when the URL can't be found etc
}
});
答案 1 :(得分:0)
我测试了你的代码,在process.php中发现了一个不合逻辑的项目(处理了一个案例):
//...
case 'getsignedagreements':
//...
//...
case 'getsignedagreements':
//...
==&gt; 应更正为(处理两种情况):
//...
case 'getnewagreements':
//...
//...
case 'getsignedagreements':
//...