如果电子邮件存在,我试图传递错误然后发送错误,说它存在于AJAX但代码不工作?

时间:2017-05-10 21:29:04

标签: php jquery arrays json ajax

这是我对ajax的代码,每件事都有效,但它不会发送电子邮件存在的错误。当我在控制台日志中查看它时,它会在createuser.php页面上给出错误。

$(document).ready(function(){
$("#signup_button").click(function(){
    if (!$("#terms").is(":checked")){
        alert("you must agree to terms");
        return false;
    }
    $.ajax({
        url: "system/api/createuser.php",
        type: "post",

        data: {email: $("#signup_email").val(), name: $("#signup_name").val(), password: $("#signup_password").val()}
    }).done(function(response){
        var resp = $.parseJSON(response);
        if (resp.errors != ""){
            alert(resp.errors);
        }else{

        }

       });
    });
});

这是createuser.php的php代码。

<?php
include ("../include.php");
$return = array();
$email = $_POST['email'];
$password = $_POST['password'];
$name = $_POST['name'];

$user = User::createuser($email, $password, $name, 1,1,1,1,1,1,1,1,1,2,2,0);

if ($user == false){
    $return['errors'] = "A user with that email exits!!";
}else{
    $return = "";
    $_SESSION['user'] = $user;
    echo "This is a test: " . $user->email . "   " . $user->name . "\n";
}
echo json_encode($return);
?>

这是createuser函数。

    global  $db, $user_id;
    // check that email is active //

    $check = $db->query("SELECT * From users where email = '".$email."'");
    $check21 = $db->fetch($check);

    if(isset($check21['email'])){
        return false;
        $db->error("user exits");
    }else{
        $check2 = $db->fetch($check);
        $options = [
    'cost' => 11,
    'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM),
    ];
    $hashed_password = password_hash($password, PASSWORD_BCRYPT, $options);



    $user_created = $db->query("INSERT INTO users (email, password, name, strength, creativity, stealth, integrity, investigation, charm, fundraising, intimidation, manipulation, job_id, party_id, admin) VALUES  ('$email','$hashed_password', '$name', 
                '$strength','$creativity', '$stealth', '$integrity',
                '$investigation', '$charm', '$fundraising', '$intimidation',
                '$manipulation', '$job', '$party', '$admin' )");


            if($user_created == true){

                $users = $db->fetch($db->query("SELECT user_id From users where email='".$email."' ") );
                $user_id = $users['user_id'];

                return new User($user_id);
            }elseif($user_created == false)
                return false;
            }else{
                return false;
            }
    }

0 个答案:

没有答案