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

Tuesday, September 24, 2013

JQuery weird bug variable not null after load() called

Today my web based application did something weird, the variable in div element doesnt nulled after load().
<div id = "content">
<input type = "text" id = "name">
<input type = "button" id = "submit">
</div>

$(document).ready(function(){
     $("#submit").click(function(){
        $("#content").load("./part/add_name.php", {
            'name' : $("#name").val()
        });
    });
});

You can still get value name input after load() to content div.

Possible solution is always call empty.
Example : 
$("#content").empty().load("./part/add_name.php", {
      'name' : $("#name").val()
});