I've the following array in PHP:
$user_data = Array
(
[session_id] => 30a6cf574ebbdb11154ff134f6ccf4ea
[ip_address] => 127.0.0.1
[user_agent] => Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
[last_activity] => 1326795168
[user_id] => 1
[username] => praditha
[logged_in] => 1
[user_status] => 1
[email] => [email protected]
)
and then in javaScript, I encode that array to json using:
var userData = '<?php echo json_encode($user_data); ?>';
and I using firebug to see userData value and this is the result:
"{"session_id":"30a6cf574ebbdb11154ff134f6ccf4ea","ip_address":"127.0.0.1","user_agent":"Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1","last_activity":1326795168,"user_id":"1","username":"praditha","logged_in":true,"user_status":"1","email":"[email protected]"}"
and the question is how di I access the array of userData
?
For example I wanna get the username
of the userData
.
I've the following array in PHP:
$user_data = Array
(
[session_id] => 30a6cf574ebbdb11154ff134f6ccf4ea
[ip_address] => 127.0.0.1
[user_agent] => Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
[last_activity] => 1326795168
[user_id] => 1
[username] => praditha
[logged_in] => 1
[user_status] => 1
[email] => [email protected]
)
and then in javaScript, I encode that array to json using:
var userData = '<?php echo json_encode($user_data); ?>';
and I using firebug to see userData value and this is the result:
"{"session_id":"30a6cf574ebbdb11154ff134f6ccf4ea","ip_address":"127.0.0.1","user_agent":"Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1","last_activity":1326795168,"user_id":"1","username":"praditha","logged_in":true,"user_status":"1","email":"[email protected]"}"
and the question is how di I access the array of userData
?
For example I wanna get the username
of the userData
.
6 Answers
Reset to default 4Since you wrapped your json_encode
output in quotes, $userData
is a string instead of an object. Losing the quotes will create a Javascript object:
var userData = <?php echo json_encode($user_data); ?>;
alert(userData.username);
userData.session_id;
userData.ip_appress;
and so on.
Don't quote it. Just
var userData = <?php echo json_encode($user_data); ?>;
then you can access userData.session_id
and so on.
You simply need to remove the quotes around the string in Javascript. So change:
var userData = '<?php echo json_encode($user_data); ?>';
to:
var userData = <?php echo json_encode($user_data); ?>;
josn_encode()
outputs a Javascript object literal, so it is valid Javascript code. By surrounding it with quotes you simply populate a string with this data, but if you remove them it will work.
use it as
<?php
$data = array('index1'=>'value1','index2'=>'value2');
?>
<script>
var json_data = <?php echo json_encode($data);?>;
alert(json_data['index1']);
alert(json_data['index2']);
</script>
<DIV id="test"><?echo json_encode($_POST);?></div>
<div id="ajax">
</div>
<script>
var sample=$("#test").text();
var obj = jQuery.parseJSON(sample);
$("#ajax").load("ceva.php",obj);
</script>
Just a script I used to get data from a div and send it to a script using $_POST. Use jQuery