How could PHP create a list for Javascript to reference?

Discussion in 'PHP' started by zipper7, Apr 14, 2008.

  1. #1
    Newbie question here


    Summary:
    I want to have javascript image rollovers on my page. I need the image source for the javascript to be PHP variables from my directory.

    Q1: Can you use php in js?:

    HYPOTHEDICAL javascript EXAMPLE:
    a onmouseover='main_image.src= <?php echo $variable ?> "onmouseout='main_image.src=" rollover01.src=<?php echo $variable ?>.><img name="rollover01" src="=<?php echo $varible ?> border="0">​


    IF so???

    Q2 How do I get this php to create a list of variables?

    Here is where the varible list needs to come from:


    if ($handle = opendir($big)) {

    while (false !== ($file = readdir($handle))) {

    if ($file != "." && $file != ".." && $file != ".txt" && $file != rtrim($big,"/" ) {

    $files[] = $file;​
     
    zipper7, Apr 14, 2008 IP
  2. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Q1:
    You can not use PHP "in" JS. PHP is executed server side, the result (HTML/JS) is executed client side.
    Your example (except for some missing quotes) is possible. But it's not what your browser will receive. When the code arrives to your browser, the PHP code will already be executed and replaced by the result.
    So instead of
    main_image.src="<?php echo $variable; ?>"
    PHP:
    your browser will receive (for example)
    main_image.src="path/to/image.jpg"
    Code (markup):
    Q2:
    If you got the files in your $files array, then you can use PHP to create the JS code you need (echo statements). But I'm not sure what you want to achieve here, and I'm not that good with JS.
     
    CreativeClans, Apr 15, 2008 IP