Tuesday, October 1, 2013

How To Read JSON from external PHP in JQuery on success

Lets say you want to check if your query successfuly executed.
 The javascript JQuery whould be like this :
$.ajax({
        type: "POST", url: "./update.php",
        data : { 'id' : $("#id").val() },
        success: function(data)
        {
                var obj = jQuery.parseJSON(data);
                 alert(obj[0].result);
        }
});

And the PHP script of update.php like this :
include "./includes/database.php";
if (mysql_query("select id from table where id = '".$_POST['id']."'"))
{
        $return[] = array('result' => 'id exists');
}
else
{
        $return[] = array('result' => 'id not found');
}
echo json_encode($return);

No comments:

Post a Comment