我试图创建一个检查数据库中是否存在某些内容的php函数,并返回" 1"或" null"基于此检查。它成功返回" 1"在第一次检查是否找到匹配但在此之后它总是返回" 1"直到页面未刷新。
所以,我认为这与缓存或类似的东西有关。我使用jQuery / AJAX从我的HTML页面提交表单。
让我用我的代码更详细地阐述一下。
functions.php(检查用户名功能):
if(!defined('INCLUDE_CHECK')) die('You are not allowed to execute this file directly');
// some functions
function checkUser($email){
$userFound = null;
// check condition with if and for-each
foreach.......
if(......
// user found
$userFound = 1;
break;
}
}
return $userFound;
}
现在我将此文件包含在我的" main.php"与
define('INCLUDE_CHECK',true);
include('functions.php'); // also tried : require 'functions.php';
$email = $_POST['email'];
并将其初始化为:
if(!checkUser($email)){
// perform action when user is not found
}else{
// echo an error msg
}
这是负责这种情况的整个代码,现在任何人都可以告诉我如何在每次提交时重置函数的返回值?或任何解决方案。
更新(HTML code):
$('html').on('click','.submit',function(e){
window.clearTimeout(formreply);
var $frm = $(this).parents('form');
var serial = $frm.serialize();
var name = $(this).attr('name');
$.ajax({
url: $frm.attr('action')+'?'+name+'=true',
type: $frm.attr('method'),
data: serial,
success: function(response){
if($frm.find('#formreply').length <= 0){
$frm.prepend('<span id="formreply"></span><hr/>');
}
if(response.err){
$frm.find('#formreply').empty().attr('class','formerr').html(response.err);
return false;
}else if(response.info){
$frm.find('#formreply').empty().attr('class','forminfo').html(response.info);
}else if(response.msg){
$frm.find('#formreply').empty().attr('class','formmsg').html(response.msg);
}
},
error: function(jqXHR, textStatus, errorThrown){
if($frm.find('#formreply').length <= 0){
$frm.prepend('<span id="formreply"></span><hr/>');
}
$frm.find('#formreply').empty().attr('class','formerr').text(textStatus);
}
});
formreply = window.setTimeout(function(){
$('#formreply,hr').empty().remove();
},4500);
e.preventDefault();
});
更新(为更好的评估提供更多代码(main.php)):
define('INCLUDE_CHECK',true);
include('functions.php');
header('Content-Type: application/json');
$admin = array(
'err'=>null,
'info'=>null,
'msg'=>null
);
// more vars & REQUESTS
if($_REQUEST['Register']){
if(strlen($name)<4 || strlen($name)>32){
$admin['err'] .= "Your name must be between 3 and 32 characters!";
}
if(!checkName($name)){
$admin['err'] .= "Your name contains invalid characters!";
}
if($email){
if(!checkEmail($email)){
$admin['err'] .= "Your email is not valid!";
}
}else{
$admin['err'] .= "Please enter your Email ID.";
}
if(strlen($password)<6 || strlen($password)>32){
$admin['err'] .= "You must enter your password!! (6-32)";
}
if(!count($admin['err'])){
if(checkUser($email) !== 1){ // also tried "!checkUser($email)"
$admin['msg'] = "Thanks".checkUser($email);
}else{
$admin['info'] = "echoing info ".getName($email).checkUser($email);
}
}
}
echo json_encode($admin);
答案 0 :(得分:0)
您的break
if
function checkUser($email) {
$userFound = null;
// check condition with if and for-each
foreach.......
if(...) {
// user found
$userFound = 1;
break;
}
}
return $userFound;
}
答案 1 :(得分:0)
你在做什么?如果您从未将$ userFound重置为null,则在第一次更改后它将始终为1。当你将它改为1时你可以突破foreach,当你将它改为1时返回函数(你可以只返回1而不是设置临时变量),或者在if条件之后将其设置为null(尽管如果你没有做任何事情,我认为这可能毫无意义。)