CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2009
    Posts
    160

    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 ?

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    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.

  3. #3
    Join Date
    May 2009
    Posts
    160

    Re: passing json data to javascript function from php

    Quote Originally Posted by PeejAvery View Post
    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.

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    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("&quot;") > -1) {
        param = param.replace("&quot;", "\"");
      }

      alert(param.toString());
    }
    </script>

    <?php
    $arr 
    = array(=> 'john'=> 'smith'=> '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.

  5. #5
    Join Date
    May 2009
    Posts
    160

    Re: passing json data to javascript function from php

    Quote Originally Posted by PeejAvery View Post
    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("&quot;") > -1) {
        param = param.replace("&quot;", "\"");
      }

      alert(param.toString());
    }
    </script>

    <?php
    $arr 
    = array(=> 'john'=> 'smith'=> '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.

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    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.

  7. #7
    Join Date
    May 2009
    Posts
    160

    Re: passing json data to javascript function from php

    Quote Originally Posted by PeejAvery View Post
    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

  8. #8
    Join Date
    May 2002
    Posts
    10,943

    Re: passing json data to javascript function from php

    PHP Code:
    <script type="text/javascript">
    var myArray = new Array();
    <?php
    $arr 
    = array(=> 'john'=> 'smith'=> '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
  •  





Click Here to Expand Forum to Full Width

Featured