0

I hve problem with jquery, i have code php like this

if($record2==0)
{
if($tmp_curr==$curr)
{
$reqvendor->inserttmp($json);
$json[] = array('stat' => true, 'msg' => '');
//$app['data'] = true;
//var_dump($reqvendor->inserttmp($json));
}
else
{
$json[]= array('stat' => false, 'msg' => 'Currency not same');
//$app[0] ='satu';
} 
}
else
{
$json[] = array('stat' => false, 'msg' => 'PO Allready Input, try another PO');
}
//var_dump($app);
echo json_encode($json);

and i have jquery like this

$.ajax
({
type: "POST",
url: host+"datatable/tmp_po",
data: dataString,
cache: false,
success: function(json)
{
if(json['stat']=="false")
{
swal({ 
title: json['msg'],
type : "warning"
});
}
else
{
$('#tbl_tmppo').DataTable().ajax.reload();
}
}
});

What i susposed is if json['stat'] is false, json['msg'] will show, but my problem is i cant call json['stat'] or json['msg'], it will always show undi

fined on console. If i console.log(json) it will show {"data":true,"msg":""} How do i call json['stat'] or json['msg']? Did i'm doin worng on jquery or on php???

EDIT I try

json.stat reslust undefined
json[0] result "{" //dont why show that :/
json[stat] result not found
json["stat"] result undefined
json['stat'] result undefined
json resulst {"data":true,"msg":""}

UPDATE I'm using this solution Get data from php array - AJAX - jQuery. and it's work!!!

Community
  • 1
  • 1
Wolfzmus
  • 463
  • 1
  • 10
  • 23

1 Answers1

0

There is a datatype mismatched. json['stats'] you are returning from PHP is boolean type. but in your if(json['stat']=="false") for double quote false is string.

so you can fix that with if(json['stat'] === false).

just remove double quotes(") from false in your if condition.

Shaharia Azam
  • 1,948
  • 19
  • 25
  • I think u not quite read my question, what i'm aks is how too call json spesific? Did i type json['stat'] or json["stat"] or json.stat?? – Wolfzmus Mar 07 '17 at 11:09
  • @Wolfzmus first of all you need to parse the json response and then only you can use `json.stat` to compare the value. hope it helps!!! – prakash tank Mar 07 '17 at 11:16
  • @prakashtank ummm.. how do i do that on jquery or php?? I'm kinda not master on this coding. – Wolfzmus Mar 07 '17 at 11:18
  • var obj = JSON.parse('{"data":true,"msg":""}'); then you can access obj.data or obj.msg – Shaharia Azam Mar 07 '17 at 11:19
  • @Wolfzmus : same as @shaharia mentioned. in your case it would be : `var res = jQuery.parseJSON(json);` `console.log(res.stat);` – prakash tank Mar 07 '17 at 11:20
  • @Wolfzmus : for more details refer the link : http://stackoverflow.com/questions/8951810/how-to-parse-json-data-with-jquery-javascript – prakash tank Mar 07 '17 at 11:22