I’m trying to return a value from PHP to JavaScript through responseText
. The idea is if mysqli_num_rows($rslt) != 0
, responseText = 1
, or if mysqli_num_rows($rslt)= 0
, to do an insert and responseText = 0
, and then in JavaScript I get the responseText
. How can I achieve that, because I tried with echo, but I couldn’t find a solution.
JavaScript code:
JavaScript
x
28
28
1
const request = $.ajax({
2
url: "Gab.php",
3
type: "POST",
4
dataType: 'json',
5
data: {username: username, password: password,email:email},
6
success: function(data){
7
console.log(data);
8
}
9
});
10
11
request.done(done);
12
request.fail("Couldnt register the user");
13
14
15
//event.preventDefault();
16
}}
17
18
function done(responseText){
19
console.log(responseText);
20
if(responseText == 0){
21
alert("Successful Registration");
22
window.location.assign("index.php");
23
}else{
24
alert("There is a username or email already registered. Change that USERNAME OR EMAIL!!!");
25
}
26
}
27
28
PHP code:
JavaScript
1
28
28
1
//connect to db
2
include 'debug.php';
3
4
$Uusername=$_POST["username"];
5
$Uemail=$_POST["email"];
6
$Upassword1=$_POST["password"];
7
8
//cleanup for db:
9
$username = mysqli_real_escape_string($db, $Uusername);
10
$email = mysqli_real_escape_string($db, $Uemail);
11
$password_1 = mysqli_real_escape_string($db, $Upassword1);
12
13
//check db for existing user or email
14
$user_check="SELECT Usersusername,Usersemail FROM users WHERE Usersusername = '$username' or Usersemail = '$email'" ;
15
$rslt = mysqli_query($db,$user_check);
16
$exist = mysqli_num_rows($rslt);
17
18
if(mysqli_num_rows($rslt) != 0){
19
echo 1;
20
//$PHPVar = 1;
21
}else{
22
$pwd= password_hash($password_1,PASSWORD_DEFAULT);
23
$result = "INSERT into users(Usersusername,Usersemail,Userspwd) VALUES ('$username','$email','$pwd')";
24
mysqli_query($db,$result);
25
echo 0;
26
//$PHPVar = 0;
27
}
28
Advertisement
Answer
Perhaps the following might help – I rearranged things a little and corrected the js functions and sent a basic JSON string as the responseText for convenience
JavaScript
1
54
54
1
<?php
2
if( $_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['username'],$_POST['password'],$_POST['email']) ){
3
ob_clean();
4
# imagine the SQL query has been executed OK but for randomness here the response will
5
# be either 1 or 0 ...
6
7
$i=mt_rand(0,1);
8
9
exit(json_encode(array('status'=>$i)));
10
}
11
?>
12
<!DOCTYPE html>
13
<html lang='en'>
14
<head>
15
<meta charset='utf-8' />
16
<title></title>
17
<script src='//code.jquery.com/jquery-latest.js'></script>
18
<script>
19
let username='fred';
20
let password='banana';
21
let email='fred@bedrock.com';
22
23
24
function done( r ){
25
console.log(r);
26
27
if( r.status == 0 ){
28
alert('Successful Registration');
29
//window.location.assign('index.php');
30
}else{
31
alert('There is a username or email already registered. Change that USERNAME OR EMAIL!!!');
32
}
33
}
34
35
$.ajax({
36
url:location.href, //'Gab.php'
37
type: 'POST',
38
dataType: 'json',
39
data: {'username': username, 'password':password, 'email':email },
40
success: function(data){
41
console.log(data);
42
done( data )
43
},
44
error:function(err){
45
alert(err)
46
}
47
});
48
</script>
49
</head>
50
<body>
51
52
</body>
53
</html>
54