Jump to content

dagogo

New Members
  • Posts

    2
  • Joined

  • Last visited

dagogo's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Warning</b>: Illegal string offset 'username' in <b>C:\Users\Ilamini\Desktop\xampp\htdocs\church-app\user.php</b> on line <b>55</b> vardump object(stdClass)#6 (10) { ["user_id"]=> string(1) "1" ["username"]=> string(11) "dagogodboss" ["password"]=> string(64) "eef4bc1edd0813b2525e1d5cbfeb0e331b9be1a7adc1fce6de64a52068412973" ["salt"]=> string(32) "jjWDzhU$%*" ["joined"]=> string(19) "2015-09-25 17:15:40" ["last_edit"]=> string(19) "0000-00-00 00:00:00" ["fullname"]=> string(25) "ilamini Ayebatonye Dagogo" ["group"]=> string(1) "1" ["home_address"]=> string(7) "maccoba" ["phone_number"]=> string(11) "08132841856" }
  2. I am doing something i don't understand how. written a php code in O.O.P and the value gotten from it are objects. but i want to convert this O.O.P object to JSON data to be used in by javascript. so I converted my converted my objects to array on the php end. the try to use the json_encode function on it the script just keep returning errors. so i tried using a function i scope out, it worked but the js keeps on rejecting the data. Below is the JS file var ajax = new XMLHttpRequest(); ajax.open('GET','user.php',true); ajax.setRequestHeader("Content-type","application/json"); ajax.onreadystatechange = function(){ if(ajax.readyState == 4 && ajax.status ==200){ var data = JSON.parse(ajax.responseText.trim()); console.log(data); console.log(data[username]); } } ajax.send(); I am doing something i don't understand how. written a php code in O.O.P and the value gotten from it are objects. but i want to convert this O.O.P object to JSON data to be used in by javascript. so I converted my converted my objects to array on the php end. the try to use the json_encode function on it the script just keep returning errors. so i tried using a function i scope out, it worked but the js keeps on rejecting the data. Below is the JS file var ajax = new XMLHttpRequest(); ajax.open('GET','user.php',true); ajax.setRequestHeader("Content-type","application/json"); ajax.onreadystatechange = function(){ if(ajax.readyState == 4 && ajax.status ==200){ var data = JSON.parse(ajax.responseText.trim()); console.log(data); console.log(data[username]); } } ajax.send(); it will return this error "SyntaxError: JSON.parse: bad control character in string literal at line 1 column 129 of the JSON data" without the JSON.parse it return undefind fro the data.username console log. Below is the PHP SCRIPT //header("Content-type: application/json"); require_once 'core/init.php'; function array2json($arr){ /*if (function_exists('json_encode')) { echo "string"; return json_encode($arr); }*/ $pars = array(); $is_list = false; $keys = array_keys($arr); $max_length = count($arr) -1; if(($keys[0] == 0) and ($keys[$max_length]==$max_length)){ $is_list = true; for($i =0; $i<count($keys);$i++){ if($i!= $keys[$i]){ $is_list = false; break; } } } foreach ($arr as $key => $value) { if(is_array($value)){ if($is_list)$parts[]= array2json($value); else $part[] = '"'.$key.':'.array2json($value);} else{ $str=''; if(!$is_list)$str ='"'.$key.'"'.':' ; if(is_numeric($value))$str .= $value; elseif($value ===false)$str .= 'false'; elseif($value === true)$str .='true'; else $str .= '"'.addslashes($value). '"'; $parts[] = $str; } } $json = implode(',', $parts); if($is_list)return '['.$json.']'; return'{'.$json.'}'; } $user = new User(); $json = array(); if(!$user->is_LOggedIn()){ echo"false"; } else{ foreach ($user->data() as $key => $value) { $json[$key] = $value; //$json =json_encode($json,JSON_FORCE_OBJECT); //echo $json; } /*$details = '{"'.implode('", "', array_keys($json)).'"'; $data = '"'.implode('" "', $json).'"}'; die($details.' / '.$data);*/ $json = array2json($json); print $json; } PLEASE HELP ME OUT TO SORT THIS ERROR THANK YOU. HOPE A PLACE THIS IN THE Right THREAD
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.