#1
  1. No Profile Picture
    The Sandman
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Location
    Ouderkerk, The Netherlands
    Posts
    46
    Rep Power
    14

    javascript popup with php variable


    How come this script doesn't work?

    PHP Code:
    <?

    "<script language=\"JavaScript\">
    function view(var){

    $p = document.write(var);
    $size=@getimagesize($p);
    $w=$size[0];
    $h=$size[1];

    window.open(\"popup.php3?p=
    $p\",\"MyWindow\",
    \"width=
    $w,height=$h,menubar=no,resizable=no,scrollbars=no,titlebar=no,toolbar=no,left=no,top=no\") }
    </script>"
    ;

    print 
    "<a href=\"javascript:view('brianmolko')\">link</a>";
    ?>
    thanx!
    Did I confuse you? I never ment to throw sand in your eyes.

    The Sandman
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Posts
    464
    Rep Power
    14
    Um, echo?
    :-)
  4. #3
  5. No Profile Picture
    The Sandman
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Location
    Ouderkerk, The Netherlands
    Posts
    46
    Rep Power
    14
    Echo what?! If I hit te link I get an error : object expected.
    Did I confuse you? I never ment to throw sand in your eyes.

    The Sandman
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    WNY
    Posts
    207
    Rep Power
    13
    echo the javascript. You have the javascript within the <? ?> tags but are not printing it to the page. Therefore, it is not being sent to the browser to be executed. Remember, Javascript is run client side, not on the server.
    strrev ("grebmil")
  8. #5
  9. No Profile Picture
    The Sandman
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Location
    Ouderkerk, The Netherlands
    Posts
    46
    Rep Power
    14
    If I echo the whole script it still won't work: object expected
    Did I confuse you? I never ment to throw sand in your eyes.

    The Sandman
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Posts
    464
    Rep Power
    14
    Hi,

    Actually you may have to change the code a bit, I'm not that good as javascript but the php commands in the middle of the function wont work.
    PHP Code:
    <?
    $image
    ='brianmolko';

    $info=@getimagesize($image);
    $w=$info[0];
    $h=$info[1];
    $size=$info[3];

    echo 
    "<script language=\"JavaScript\">
    function view(var){
    window.open(\"popup.php3?p=
    $p\", \"MyWindow\",
    \"width=
    $w, height=$h, menubar=no, resizable=no, scrollbars=no, titlebar=no, toolbar=no, left=no, top=no\") }
    </script>"
    ;

    print 
    "<a href=\"java script:view('$image')\">link</a>";
    ?>
    Bye ...
    :-)
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Posts
    790
    Rep Power
    16
    Try removing the space between java and script


    This:
    print "<a href=\"java script:view('brianmolko')\">link</a>";

    Should be:
    print "<a href=\"javascript:view('brianmolko')\">link</a>";
  14. #8
  15. No Profile Picture
    The Sandman
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Location
    Ouderkerk, The Netherlands
    Posts
    46
    Rep Power
    14
    What I want is a popupscript for one popup (popup.php3) and a page with several links like
    PHP Code:
    <?
    print "<a href=\"javascript:view('image1')\">link</a>";
    print 
    "<a href=\"javascript:view('image2')\">link</a>";
    ?>
    . When I hit the link it should open the popup page with width and height adjusted to the imagesizes.
    Did I confuse you? I never ment to throw sand in your eyes.

    The Sandman
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Location
    WNY
    Posts
    207
    Rep Power
    13
    I think derrickatdev's code should work with 2 changes:

    1. You need to properly specify the image file. -> brianmolko.gif or brianmolko.jpg etc.

    2. Lose the space as micros_bytes suggests.

    If that does not do the trick try any of the following:

    You may want to check the source in your browser after the script is parsed and make sure the js is formated correctly.

    Try doing a vardump() of your variables to make sure they are correct.

    You could also make a copy of the js with the correct set of values hard coded in for a test image. After you get it working, replace the values with your php variables and insert into you script. That way you will know its not the js that is causing the problem.
    strrev ("grebmil")
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Posts
    464
    Rep Power
    14
    Hi,

    Had a bit of a play around with your code and came up with this.

    Your main page containing the links to the images
    PHP Code:
    <?php

    // relative url to images, if in the same directory leave blank
    $imagedirectory='./images/';

    // your images, just fill in the array, no limit
    $images[0]=$imagedirectory '1013.jpg';
    $images[1]=$imagedirectory '1868.jpg';
    $images[2]=$imagedirectory '3097.jpg';

    ?>
    <script language="JavaScript"> 
    <!--
        // to give a margin around the image in the popup set here. set to 0 for no margin
        showmargin=5;

        function view(showimage,w,h) {
            w = w + showmargin * 2;
            h = h + showmargin * 2;
            window.open("popup.php3?p=" + showimage + "&m=" + showmargin, "MyWindow", "width=" + w + ", height=" + h + ", menubar=no, resizable=no, scrollbars=no, titlebar=no, toolbar=no, left=no, top=no") 
        }
    // -->
    </script>
    <?php

    // loop through the images array to get the dimensions of each
    foreach($images as $image) {
        
    $info=@getimagesize($image);
        
    $w=$info[0];
        
    $h=$info[1];
        
    $size=$info[3];

        
    // the link to each image
        
    print "<p><a href=\"javascript:view('$image',$w,$h)\">link</a>";
    }

    ?>
    The popup window
    PHP Code:
    <html>
    <head>
    <title><?php echo $p?></title>
    </head>
    <body TOPMARGIN=<?php echo $m?> LEFTMARGIN=<?php echo $m?> MARGINWIDTH=0 MARGINHEIGHT=0>
    <?php
    echo '<img src="' $p '" border=0>';
    ?>
    </body>
    </html>
    PS. How do you get to display the word javascript without the space?
    PPS. Even weirder, no space in the PS but still space in the code above.
    Bye ...
    Last edited by derrickatdev; March 8th, 2003 at 09:48 AM.
    :-)
  20. #11
  21. No Profile Picture
    The Sandman
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Location
    Ouderkerk, The Netherlands
    Posts
    46
    Rep Power
    14
    Thanx for the code! I really don't know how you can get javascript without the space between it.

    If you add it between the php tags then you will get this:

    PHP Code:
     print "<p><a href=\"javascript:view('$image',$w,$h)\">link</a>"
    The script must add a space between it or something. Weird!!
    Did I confuse you? I never ment to throw sand in your eyes.

    The Sandman

IMN logo majestic logo threadwatch logo seochat tools logo