此时用户使用注册表格进行注册 如果电子邮件已经存在于数据库中,则会显示“已存在的电子邮件”消息,其他用户可以完成注册。 但不能显示消息我错了什么?
我的控制器
public function emailCheck()
{
$this->autoRender = false;
$this->viewBuilder()->layout('false');
if ($this->request->is(['post'])) {
$search =$this->request->data('email');
$query = $this->Users->find('list', [
'conditions' => ['Users.email LIKE '=>'%' .$search. '%'],
'limit' => 1
])->all();
foreach ($query as $key) {
if($key!=null){
echo "This email has been taken!!";
}else
echo "Email avilable.";
}
}
}
我的Ajax
<script>
$('document').ready(function(){
$('input[name="email"]').change(function(){
var email = $(this).val();
$.ajax({
method:'POST',
url:'<?php echo Router::url(['controller' => 'Users', 'action' => 'emailCheck']); ?>',
data:{search:email},
success: function(data)
{
alert(data);
}
});
})
})
答案 0 :(得分:2)
如果我理解正确:
您声明var ex = OwinRequestScopeContext.Current.Items["error"] as Exception;
,但是以find('list')
结束查询,返回一个对象数组(即使您将其限制为1)。因此,all()
永远不会只是$key
。
但是,您可以尝试使用给定的电子邮件找到第一条记录:
null
注意:强>
我还从您的查询中删除了$query = $this->Users->find('list', [
'conditions' => ['Users.email LIKE'=> $search],
])->first();
if (is_null($query)) {
// the email is still available
} else {
// the email is already in use
}
通配符:将它们留在那里会导致误报,例如:当有人订阅%
时,123johndoe@gmail.com
会收到他的电子邮件已被使用的消息}
答案 1 :(得分:0)
最后我得到了解决方案。解决方案是 我的控制器
public function emailCheck()
{
$this->autoRender = false;
$this->viewBuilder()->layout('false');
if ($this->request->is(['post'])) {
$email =$this->request->data('email');
$query = $this->Users->find()
->where(['Users.email '=>$email])->count();
if($query) {
echo 1;
}
}
}
我的Ajax
<script>
$('document').ready(function(){
$('#email').keyup(function(){
var email = $(this).val();
$.ajax({
method:'POST',
url:'<?php echo Router::url(['controller' => 'Users', 'action' => 'emailCheck']); ?>',
dataType: "text",
data:{email:email},
success: function(data)
{
if(data){
$( ".result" ).html('<span style="color:red">Someone already has that username. Try another?</span>');
}else{
$( ".result" ).html('<span style="color:green">The email is still available!!</span>');
}
}
});
})
})
这些脚本完全用于检查重复的email.testing由我