So, here's my dilemma. I know the .js file is working in that it sends the json data to the PHP file, and the PHP receives it and executes the SQL command. The developer view shows me this is running properly. My issue is that in my 'function (data) {}' section of the JS file, I'm unsure on how to manipulate the JSON array properly i.e. I dont know how to check if the json value returned == 'success'.
Any help or tips would be greatly appreciated!
JS Code:
$(document).ready(function(){
$('#login_button').click(function(){
//get values from input text boxes (Email & Password)
var email = $('#email').val();
var password = $('#password').val();
$.post('http://localhost:8888/php/login.php', {email1:email, password1:password}, function(data) {
$("#login_button").html(result); //print JSON returned
if (result[0] == "success"){ //check if 'success' returned by PHP file
console.log(data);
alert('working');
window.location.replace("http://localhost:8888/index.html"); //
} else {
console.log(data);
alert('error');
}
// console.log(data);
}, "json");
});//eo login_button
});//eof
PHP Code:
<?php
//server info
$servername = "localhost";
$username = "root";
$dbpassword = "root";
$dbname = "personal_data";
//Establish server connection
$conn = new mysqli($servername, $username, $dbpassword, $dbname);
//Check connection for failure
if (mysqli_connect_errno()) {
$error = (mysqli_connect_error());
echo "error";
exit();
}
print_r($_POST);
//Read in email & password
$email = mysqli_real_escape_string($conn, $_POST['email1']);
$password = mysqli_real_escape_string($conn, $_POST['password1']);
// echo $email;
// echo $password;
$sql = "SELECT Name, Age FROM personal_data WHERE Email='$email' AND Password='$password' LIMIT 1";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
$jsonString = array('result' => 'success');
echo json_encode($jsonString);
} else {
$jsonString = array('result' => 'failure');
echo json_encode($jsonString);
}
mysqli_close($conn);
?>
In the Success function, you should be used data
$.post('http://localhost:8888/php/login.php', {email1:email, password1:password}, function(data) {
$("#login_button").html(data); //print JSON returned
if (data[0] == "success"){ //check if 'success' returned by PHP file
console.log(data);
alert('working');
window.location.replace("http://localhost:8888/index.html"); //
} else {
console.log(data);
alert('error');
}
});