如何从php中的echo获取responseText

我正在尝试通过 responseText 将值从 PHP 返回到 JavaScript。这个想法是如果 mysqli_num_rows($rslt) != 0responseText = 1mysqli_num_rows($rslt)= 0 进行插入和 responseText = 0,然后在 JavaScript 中我得到 responseText。我怎样才能做到这一点,因为我尝试使用 echo,但找不到解决方案。

JavaScript 代码:

const request = $.ajax({
            url: "Gab.php",type: "POST",dataType: 'json',data: {username: username,password: password,email:email},success: function(data){
                console.log(data);
            }
        });
        
        request.done(done);
        request.fail("Couldnt register the user");
        
        
        //event.preventDefault();
    }}
    
    function done(responseText){
        console.log(responseText);
        if(responseText == 0){
            alert("Successful Registration");
            window.location.assign("index.php");
            }else{
            alert("There is a username or email already registered. Change that username OR EMAIL!!!");
        }
    }

PHP 代码:

//connect to db 
include 'debug.php';

$Uusername=$_POST["username"];
$Uemail=$_POST["email"];
$Upassword1=$_POST["password"];

//cleanup for db:
$username = mysqli_real_escape_string($db,$Uusername);
$email = mysqli_real_escape_string($db,$Uemail);
$password_1 = mysqli_real_escape_string($db,$Upassword1);

//check db for existing user or email
$user_check="SELECT Usersusername,Usersemail FROM users WHERE Usersusername = '$username' or Usersemail = '$email'" ;
$rslt = mysqli_query($db,$user_check);
$exist = mysqli_num_rows($rslt);

if(mysqli_num_rows($rslt) != 0){    
    echo 1;
    //$PHPVar = 1;
    }else{
    $pwd= password_hash($password_1,PASSWORD_DEFAULT);
    $result = "INSERT into users(Usersusername,Usersemail,Userspwd) VALUES ('$username','$email','$pwd')"; 
    mysqli_query($db,$result);
    echo 0;
    //$PHPVar = 0;  
}   
JJ13404145553 回答:如何从php中的echo获取responseText

也许以下可能会有所帮助 - 为了方便起见,我稍微重新安排了一些东西并更正了 js 函数并发送了一个基​​本的 JSON 字符串作为 responseText

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['username'],$_POST['password'],$_POST['email']) ){
        ob_clean();
        # imagine the SQL query has been executed OK but for randomness here the response will
        # be either 1 or 0 ...
        
        $i=mt_rand(0,1);
        
        exit(json_encode(array('status'=>$i)));
    }
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title></title>
        <script src='//code.jquery.com/jquery-latest.js'></script>
        <script>
            let username='fred';
            let password='banana';
            let email='fred@bedrock.com';
        
        
            function done( r ){
                console.log(r);
                
                if( r.status == 0 ){
                    alert('Successful Registration');
                    //window.location.assign('index.php');
                }else{
                    alert('There is a username or email already registered. Change that USERNAME OR EMAIL!!!');
                }
            }
        
            $.ajax({
                url:location.href,//'Gab.php'
                type: 'POST',dataType: 'json',data: {'username': username,'password':password,'email':email },success: function(data){
                    console.log(data);
                    done( data )
                },error:function(err){
                    alert(err)
                }
            });
        </script>
    </head>
    <body>
        
    </body>
</html>
本文链接:https://www.f2er.com/1195889.html

大家都在问