Click to See Complete Forum and Search --> : passing json data to javascript function from php
niladhar8@gmail.com
June 8th, 2010, 03:27 PM
m not sure if this is to be posted here or in the client side section, but it has to do with php also so will post here.
how can i pass json data from json_encode to a javascript function and how can i use it in that function ?
PeejAvery
June 8th, 2010, 05:59 PM
You'll have to echo it directly into the function or one of the parameters...or pass it as a parameter in an AJAX method.
niladhar8@gmail.com
June 8th, 2010, 07:44 PM
You'll have to echo it directly into the function or one of the parameters...or pass it as a parameter in an AJAX method.
passing it directly gives me a parse error, passing it as an ajax parameter i first have to get it from my php script to javascript... explaining a little more below is what i wish to do.
$arr = ("1"=>'john', "2"=>smith, "3"=>'kerri');
$param = json_encode($arr);
echo "<span onclick=pass_json($param)";
i wish to access that json format text in javascript, to give you an idea of what i wish to accomplish is.
I am fetching data for from the data base which contains names and addresses and other details of people. On the left i have a scrollable menu of all those people with their names, so as soon as a name is clicked i want their details (address etc) displayed on the right side without any page load.... yes i do know i can do this using ajax but i donot wish to transact with server as i have already fetched al that data.
PeejAvery
June 8th, 2010, 09:12 PM
A few minor parts left off in your code...Anyway, the problem you are facing is most likely quotes inside of quotes. A simple HTML entity replace fixes the issue.
<script type="text/javascript">
function pass_json(param) {
while (param.search(""") > -1) {
param = param.replace(""", "\"");
}
alert(param.toString());
}
</script>
<?php
$arr = array(1 => 'john', 2 => 'smith', 3 => 'kerri');
$param = json_encode($arr);
?>
<span onclick="pass_json('<?php echo htmlentities($param); ?>');">Test me!</span>
niladhar8@gmail.com
June 9th, 2010, 10:10 AM
A few minor parts left off in your code...Anyway, the problem you are facing is most likely quotes inside of quotes. A simple HTML entity replace fixes the issue.
<script type="text/javascript">
function pass_json(param) {
while (param.search(""") > -1) {
param = param.replace(""", "\"");
}
alert(param.toString());
}
</script>
<?php
$arr = array(1 => 'john', 2 => 'smith', 3 => 'kerri');
$param = json_encode($arr);
?>
<span onclick="pass_json('<?php echo htmlentities($param); ?>');">Test me!</span>
Ok but how can i access it in the javascript, lets say my array was as below
$arr = array(1 => array( 'name' =>'john', 'age' => '23', 'sex' => 'male'), 2=> array( 'name' =>'kerri', 'age' => '19', 'sex' => 'female') );
lets say i want to know the name and age of person with key/index 2 (kerri 19 in this)... how would i be able to to do that. i did google but it shows me
param.1[0].name// which gives me an error.
what i wish to accomplish is pass uniqe id as the key from the database and all the subsequent data that i may have, so when a user clicks on a left navigation menu alli have to pass is this json formatted data and the unique id and using jquery i could place that content on the right div.
Firstly i wish to know how i can access that json data in javascript
secondly is this the right approach to what i wish to accomplish or is there a better way.
PeejAvery
June 9th, 2010, 01:43 PM
So then why are you using JSON? You should just output the data as an associative array in JavaScript.
niladhar8@gmail.com
June 9th, 2010, 01:47 PM
So then why are you using JSON? You should just output the data as an associative array in JavaScript.
but how can i pass an array from php to javascript.
are you saying dynamically build javaascript like
echo "<script type=java....>';
blah blah blah
??
I could do that, but thought that wasnt a good way of doing.
Anyways Is it not possible for me to access that data how i mentioned previously using JSON in javascript.
how can i out put the data as an assosciative array in js, does it work exactly like that as php ?
PeejAvery
June 9th, 2010, 08:27 PM
<script type="text/javascript">
var myArray = new Array();
<?php
$arr = array(1 => 'john', 2 => 'smith', 3 => 'kerri');
foreach ($arr as $k => $v) {
?>
myArray["<?php echo $k; ?>"] = "<?php echo $v; ?>";
<?php } ?>
</script>
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.