I have a button in php page that calls onclick a ajax function. Then I use php to update my database and return the result back to ajax. The problem is that for some reason I cannot return the result. I always get the error function and worst I don't get the 'returned.msg' value just `undefined` in my alert box
function AjaxAddPerson() {
$.ajax({
type: "GET",
url: "../lib/addPerson.php",
dataType: 'json',
data: { person: document.getElementById("PersonSearchInput").value},
success:function( returned ) {
alert( "Data Saved: " + returned.msg );
},
error:function(returned ) {
alert( "Could not Saved: " + returned.msg );
}
});
}
<?php
session_start();
define("DB_HOST", '.000webhost.com');
define("DB_USER", '_');
define("DB_PASSWORD", '');
define("DB_DATABSE", '_');
$email = $_GET['person'];
$UserEmail = $_SESSION['login'];
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_DATABSE, $conn);
$sql_find = mysql_query("SELECT * FROM Users WHERE UserEmail='".$email."'");
$UserInfo = array();
while ($row = mysql_fetch_array($sql_find, MYSQL_ASSOC)) {
$UserInfo[] = $row['UserEmail'];
}
if(in_array($email, $UserInfo)) {
$result = mysql_query("UPDATE `Users` SET `Contacts` = '".$email."' WHERE `UserEmail` = '".$UserEmail."'");
if ($result) {
$returnRes = array( 'found' => true, 'msg' => "Person added to your record");
echo json_encode($returnRes);
}
else {
$returnRes = array( 'found' => false, 'msg' => "Error adding person to your record \n Is the person emails' correct?");
echo json_encode($returnRes);
}
}
else {
$returnRes = array( 'found' => false, 'msg' => "We couldn't find the user in our databases.");
echo json_encode($returnRes);
}
?>
PS.
The code is not the best and is vulnerable to sql-injection. But this isn't my problem for now.