Click here to Skip to main content
15,064,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

JavaScript
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 );
        }
       });
}


C#
<?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.
Posted
Updated 22-Dec-15 10:07am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900