in js
file i'm sending json object php file don't know how access sent object.
first line in code below give me that:{"id":1,"email":"asd@qwe.co.uk","password":"xxxx","location":"london"}
js file
app.showalert(json.stringify(profile)); $.ajax({ type: "get", url:"http://www.domain.co.uk/test-login.php", datatype: 'jsonp', data: { data: json.stringify(profile) }, success:function(json){ // stuff json (in case array) app.showalert(json.stringify(json), "login ok"); }, error:function(){ app.showalert("login faild", "wrong username or password. please try again."); }, });
php file:
<?php header('content-type: application/json'); $ret=$_get['data']; $ret=json_decode($ret, true); echo '['.json_encode($ret[0]).']'; ?>
php test, because want check if user pass correct details, return json object 'loggedin' => 1
or so, if not 0
i tried access object $ret=$_get['profile'];
, didn't help.
my question is: how pass json object , access in php.
you need modify both ajax , php want. i've changed javascript test success/fail within success function. if returning json php, don't want handling failed password in error event.
for php seem getting input , output mixed up. can see input decoded $data
variable, , output array in $output
until gets encoded , output.
$.ajax({ type: "get", url:"http://www.domain.co.uk/test-login.php", datatype: 'jsonp', data: { data: json.stringify(profile) }, success:function(json){ // stuff json (in case array) if(json.loggedin == '1'){ alert("logged in"); } else { alert("failed login"); } } });
php:
$output = array('loggedin' => 0); $data = json_decode($_get['data']); // shows how access data if($data->email == 'asd@qwe.co.uk' && $data->password = '1234') { $output['loggedin'] = '1'; } header('content-type: application/json'); echo json_encode($output);
Comments
Post a Comment