Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jul 2006
    Posts
    110
    Thanks
    1
    Thanked 0 Times in 0 Posts

    display php array in javascript alert

    Hi everyone -
    Code:
    <form>
    <input type="button" onclick=
    "alert('<?php print_r($_SESSION);?>')" 
    value="Debug">
    </form>
    This doesn't work - and I suspect it has something to do with newlines and JavaScript string literals - but I've been unsuccessful.

    I figured since it probably requires some formatting (preg_replace or something) with php that I should post it in the php forum instead of the JS forum.

    Anyone have any tips?

  • #2
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    try

    PHP Code:
    <?PHP
    echo '<form>
    <input type="button" onclick=
    "alert(\''
    .(print_r($_SESSION)).'\')" 
    value="Debug">
    </form>'
    ;
    ?>
    You can not say you know how to do something, until you can teach it to someone else.

  • #3
    Regular Coder
    Join Date
    Jul 2006
    Posts
    110
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the help, timgolding -
    I tried this, but it just prints the array above the button.

  • #4
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Yeah I just noticed two secs
    You can not say you know how to do something, until you can teach it to someone else.

  • #5
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    This outputs all the values but not the key

    PHP Code:
    <?PHP
    $_SESSION
    ['test']=test;
    $string=implode('',$_SESSION);

    echo 
    '<form>
    <input type="button" onclick=
    "alert(\''
    .($string).'\')" 
    value="Debug">
    </form>'
    ;
    ?>
    You can not say you know how to do something, until you can teach it to someone else.

  • #6
    Regular Coder
    Join Date
    Jul 2006
    Posts
    110
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for your help -

    I guess I was just looking for an easy way to view my entire session array for debugging while working on this project - I thought a JS alert box would be nice, but I'd really like it to look like a nicely formatted array.

    Is there a better way, perhaps?

  • #7
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    This looks not so bad. However im sure there will be a funtion that formats the array into a readable string without echoing it to the screen.

    PHP Code:
    <?PHP
    $_SESSION
    ['test']='test1';
    $keys=array_keys($_SESSION);
    foreach(
    $keys as $val){
      
    $string.=$val.' => ';
      
    $string.=$_SESSION[$val].', ';
      }

    echo 
    '<form>
    <input type="button" onclick=
    "alert(\''
    .($string).'\')" 
    value="Debug">
    </form>'
    ;


    ?>
    You can not say you know how to do something, until you can teach it to someone else.

  • #8
    Regular Coder
    Join Date
    Jul 2006
    Posts
    110
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That actually looks fine - but it doesn't work the nested array I put in my session.

    I do appreciate your help, though - please don't worry about taking the time to fix this if you are busy with other things... It isn't necessary by any means... just a novelty.

  • #9
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    If you use print_r($array, 1) the output will be returned as a string so you can echo it where you want instead of printing it at the top of the page(you can use any boolean true value, not just 1). print_r()


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •