#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    9
    Rep Power
    0

    Question PHP & JavaScript


    Can you pass variables and arrays from PHP to JavaScript and how?

    Or could you use XML instead?

    peterw
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2001
    Location
    UK
    Posts
    527
    Rep Power
    14
    just use php to print out the javascript into your HTML pagee:


    echo "<script language=\"javascript\">\n";
    echo "myjavascriptvariable=".$myphpvariable.";\n";
    echo "<\script>\n";

    etc.
    Doc MacGurgles Babylonian Lizard Tooth Oil worked for me
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    9
    Rep Power
    0
    Thanks for the reply but it doesn't work.

    This the php code I put into the doc.
    echo('<script language="javascript">' ."\n");
    echo('data =' .$dbdata[0][2]. ";\n");
    echo('</script>');

    This is the output within the html doc.

    <script language="javascript">
    data =lynnew;
    </script>

    The script you sent me places the value of the variable $dbdata[0][2]. into the code so I get an undefined variable error.

    peterw
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Calabasas Ca
    Posts
    6
    Rep Power
    0
    I have seen the above solution posted many times before in different places. However, whenever I try it or variations of it I cannot get the code to work correctly. The HTML source looks correct but the javascript doesn't seem to respond correctly.
    Why doesn't the following generate an alert? I must be missing something stupid....
    <?
    $myphpvariable = "test";
    echo "<script language=\"javascript\">\n";
    echo "myjavascriptvariable=".$myphpvariable.";\n";
    echo "alert(myjavascriptvariable);\n";
    echo "<\script>\n";
    ?>
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2001
    Location
    UK
    Posts
    527
    Rep Power
    14
    The idea is to make the code printed out look like the HTML Javascript you would normally write to your browser. So if
    $dbdata[0][2] is a string you would normally write in javascript:

    date ="lynnew";

    therefore you need to organise your php to print the " (quotes)

    echo "myjavascriptvariable=\"".$myphpvariable."\";\n";
    Doc MacGurgles Babylonian Lizard Tooth Oil worked for me
  10. #6
  11. No Profile Picture
    Motown Junker
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Posts
    14
    Rep Power
    0
    Try surrounding the PHP variable output in single quotes...


    <?
    $myphpvariable = "test";
    echo "<script language=\"javascript\">\n";
    echo "myjavascriptvariable=
    '".$myphpvariable."';\n";
    echo "alert(myjavascriptvariable);\n";
    echo "<\script>\n";
    ?>r
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    9
    Rep Power
    0
    Is it possible to send a complete array to JavaScript (eg - a pointer) or do you have to send each peice of data individually?

    peterw
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2000
    Posts
    669
    Rep Power
    14
    You'll need to send each one individually although it shouldn't be that hard.

    <script language="javascript">
    var testArray= new Array(<?=count($yourarray)?>);
    <?
    $i=0;
    foreach($yourarray as $key => $val) {
    ?>
    testArray[<?=$i?>] = '<?=$val?>';
    <?
    $i++;
    }
    ?>
    </script>
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2001
    Location
    UK
    Posts
    527
    Rep Power
    14
    jrob00,

    your script fails because you probably copied my typo <\script> should be </script>
    Doc MacGurgles Babylonian Lizard Tooth Oil worked for me
  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Calabasas Ca
    Posts
    6
    Rep Power
    0
    You know, I was saving the html output and modifying it to try and figure out the problem when I noticed that...
    Thanks for looking at it - I feel like a clown

    Then funny thing is - that same exact code has been passed around to different forums so I figured something must have been wrong with the theory of it - go figure..

    thanks a lot!

IMN logo majestic logo threadwatch logo seochat tools logo