Despite everything running smoothly in the program below, an AJAX error is triggered:
javascript:
var data = {
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="026f6742656f636b6e2c616d6f">[email protected]</a>',
password: 'secretword'
};
$.ajax({
type: "POST",
dataType: "application/json",
url: "http://localhost/CFBserver/validateUser.php",
data: data,
success: function (response) {
console.log(response.responseText);
},
error: function (response) {
console.log(response.responseText);
}
});
}
php:
<?php
$conn = mysqli_connect('localhost', 'root', '', 'cfbdata');
if (mysqli_connect_errno($conn)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING);
$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
$sql = "SELECT * FROM users WHERE email = '$email' AND password = '$password'";
if (!mysqli_query($conn, $sql)) {
die('Error: ' . mysqli_error($conn));
}
$result = mysqli_query($conn, $sql);
$numrows = mysqli_num_rows($result);
if ($numrows > 0) {
$message = array('result' => 'found',
'email' => $email,
'password' => $password,
);
} else {
$message = array('result' => 'Not found',
'email' => $email,
'password' => $password,
);
}
header('Content-type: application/json');
echo json_encode($message);
mysqli_close($conn);
?>
The console output shows:
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
{"result":"found","email":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcb1b99cbbb1bdb5b0f2bfb3b1">[email protected]</a>","password":"secretword"}
</body>
</html>
Although PHP successfully retrieves the record from the MySQL database, it triggers an error upon returning to AJAX. Any ideas why?