exporting php array to js

Discussion in 'PHP' started by IndyAnna, May 29, 2009.

  1. #1
    Good day to everyone!
    I'm learning about php at school and have a project going on. I need a hand (or code) at this point.
    I wrote a php which opens a certain web-page and stores all the data needed in array => Level1 => Level2. Now I tried to open it up from jscript and it works like magic but in particular case. Let me show on a simpler example (this is not the entire code, but the idea):

    In php: $testarray=("sthasd","asds","aga");
    In JS: alert ("<?php echo $testarray[1]?>"); returns asds

    But I can't use a for loop to write all elements of array as
    In JS: for(i=0;i<<?php echo sizeof($testarray)?>i++)
    alert ("<?php echo $testarray?>");

    as php will interpret this i as $testarray which doesn't exist. I tried passing "i" and 'i' to it but it doesn't work. Any ideas on this?

    edit: I forgot to mention this all happens on 1 php page. So you have some php code executing first (population of data) then some html with onChange call to JS (which repopulates a select using options data from php) and afterwards some more php with databases, but this is not all that important I guess. I guess this sounds weird but I that's the way it should be.
     
    IndyAnna, May 29, 2009 IP
  2. Sabbir

    Sabbir Banned

    Messages:
    210
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #2
    i think you should put an ; in your for(..).

    here In JS: for(i=0;i<<?php echo sizeof($testarray)?>;i++)
     
    Sabbir, May 29, 2009 IP
  3. IndyAnna

    IndyAnna Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It was a typing error I made when posting here. There is a ; in for loop but it doesn't work nonetheless.
     
    IndyAnna, May 30, 2009 IP
  4. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You are trying to use a javascript variable (i) inside the php script, which is not possible.
    for(i=0;i<<?php echo sizeof($testarray)?>i++)
    alert ("<?php echo $testarray[[B]i[/B]]?>");
    Code (markup):
    The bolded i inside your code is actually the javascript variable, declared in the for loop. Inside the <?php ?> everything is treated as a php script, so you don't have "access" to i. I think you've misunderstood the task - check again and ask if you can't handle it :)
     
    xlcho, May 30, 2009 IP
  5. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #5
    <html>
    <head>
    <script>
    <?php
    
    $myarray = array('php', 'mysql', 'program');
    
    echo 'myarray = new Array();';
    foreach($myarray as $element){
          echo 'myarray[myarray.length] = "'.$element.'";';
    }
    
    echo 'for(i = 0;i < myarray.length;i++){';
    echo 'alert(myarray[i]);';
    echo '}';
    
    ?>
    </script>
    </head>
    <body>
     test.....
    </body>
    </html> 
    PHP:
     
    php-lover, May 30, 2009 IP
  6. johnkramlich

    johnkramlich Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I would recommend looking into jQuery if you are trying to populate a select menu with information generated by a PHP script. I've been using jQuery for the past year and I can't see myself ever going back to writing plain old Javascript.
     
    johnkramlich, May 31, 2009 IP
  7. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If you plan on using a framework such as JQuery, or Prototype, you can then easily transfer data from PHP to Javascript by using the serialize() function, the converted string can then be easily loaded by JSON in either framework (as it is often used in this manner to get data during AJAX functionality).
     
    kblessinggr, Jun 1, 2009 IP
  8. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #8
    You can serialize it and parse in $_GET or something.
     
    Kaizoku, Jun 1, 2009 IP