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!!!

3
  • use json.msg. Commented Mar 7, 2017 at 11:02
  • u didnt see my coding, is json not jsonObj, and yes i try json.msg it show undifend too. :/ Commented Mar 7, 2017 at 11:05
  • json.msg show undefined Commented Mar 7, 2017 at 11:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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 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!!!
@prakashtank ummm.. how do i do that on jquery or php?? I'm kinda not master on this coding.
var obj = JSON.parse('{"data":true,"msg":""}'); then you can access obj.data or obj.msg
@Wolfzmus : same as @shaharia mentioned. in your case it would be : var res = jQuery.parseJSON(json); console.log(res.stat);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.