-
June 8th, 2010, 03:27 PM
#1
passing json data to javascript function from php
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 ?
-
June 8th, 2010, 05:59 PM
#2
Re: passing json data to javascript function from php
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.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
June 8th, 2010, 07:44 PM
#3
Re: passing json data to javascript function from php
Originally Posted by PeejAvery
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.
Code:
$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.
-
June 8th, 2010, 09:12 PM
#4
Re: passing json data to javascript function from php
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.
PHP Code:
<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>
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
June 9th, 2010, 10:10 AM
#5
Re: passing json data to javascript function from php
Originally Posted by PeejAvery
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.
PHP Code:
<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
Code:
$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.
-
June 9th, 2010, 01:43 PM
#6
Re: passing json data to javascript function from php
So then why are you using JSON? You should just output the data as an associative array in JavaScript.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
June 9th, 2010, 01:47 PM
#7
Re: passing json data to javascript function from php
Originally Posted by PeejAvery
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 ?
Last edited by niladhar8@gmail.com; June 9th, 2010 at 01:51 PM.
Reason: change
-
June 9th, 2010, 08:27 PM
#8
Re: passing json data to javascript function from php
PHP Code:
<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>
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|