Skip to content Skip to sidebar Skip to footer

Json Encode Alert Message

How do I alert a json_encode() message and reload the page? The function below only displays an undefined alert message if($result1 == false) $response['msg'] = 'Transfer failed d

Solution 1:

You're trying to get a undefined index response

Provided that your PHP script returns:

{"msg":"<your-message-here>"}

In your javascript you can do it:

$.ajax({
    type : "POST",
    url: "transferProduct.php",
    dataType: 'json',
    success : function(response) {                     
        alert(response.msg);
        location.reload();         
    }
});

Solution 2:

Use code in this way

transferProduct.php

if($result1 == false)
 $response['msg'] = "Transfer failed due to a technical problem. Sorry.";
else$response['msg'] = "Successfully transferred";
echo json_encode($response);

code page

$("#transfer").click(function() {
 $.ajax({
 type : "POST",
 url : "transferProduct.php",
 data : {},
 success : function(data) {                     
   datas = $.parseJSON(data);
   alert(datas.msg);    
   location.reload();         
  }
 });
});

or you can use $.getJSON in place of $.ajax

$("#transfer").click(function() {
$.getJSON("transferProduct.php",function (data){
alert(data.msg);    
   location.reload();
});
});

Solution 3:

I think you should format the return to correctly identify in the background:

{"success":true,"message":"---------"}

then in JavaScript: data.message

Post a Comment for "Json Encode Alert Message"