Heim  >  Artikel  >  Backend-Entwicklung  >  求教,thinkphp与jquery,ajax表单验证

求教,thinkphp与jquery,ajax表单验证

WBOY
WBOYOriginal
2016-06-06 20:14:49855Durchsuche

图1
求教,thinkphp与jquery,ajax表单验证

图2
求教,thinkphp与jquery,ajax表单验证

图3
求教,thinkphp与jquery,ajax表单验证

在图1的邮件输入框内输入邮箱后,通过图2的JS文件将邮箱信息传递给图3的php文件判断,图3的数据库操作总是出错,没有数据库操作的话没错,不知道为什么

回复内容:

图1
求教,thinkphp与jquery,ajax表单验证

图2
求教,thinkphp与jquery,ajax表单验证

图3
求教,thinkphp与jquery,ajax表单验证

在图1的邮件输入框内输入邮箱后,通过图2的JS文件将邮箱信息传递给图3的php文件判断,图3的数据库操作总是出错,没有数据库操作的话没错,不知道为什么

1、使用ajaxReturn代替echo
2、true和false要用字符串形式返回结果,即"true"、"false"
3、通过ajax返回的结果是字符串,不能用if直接判断,而应该使用==来判断,否则一直为if(response)一直为true,所以总是返回已存在

=======================
修改答案
首先你要对mvc模型有充分的认识,才可以正确使用thinkphp。
1、thinkphp的逻辑是/控制器/方法,
假定你的模块名是Index(从你的项目目录上看应该是这样)

<code><?php namespace Index/Controller;
use Think/Controller;
class LoginController extends Controller{
    public function verify(){
        $where['email'] = $_POST['loginEmail'];
        $userModel = M("user");
        if($userModel->where($where)->find()){
            $this->ajaxReturn(true);
        }else{
            $this->ajaxReturn(false);
        }
    }
}</code>

然后你post的代码应该这样写,位置应该在thinkphp的view文件夹里

<code>$.post("/Login/verify",{loginEmail:loginEmail}, function(response){
    ...
}</code>

用ajaxReturn返回结果

false和true换成'false'和'true'试试看

$.post(url, data, function, 'josn')

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn