Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> bardzo prosty skrypt losowania obrazka, małe połączenie JS i PHP
cybertix
post
Post #1





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 28.08.2007

Ostrzeżenie: (0%)
-----


Witam

pisze sobie skrypt który wyświetla losowy obrazek


cała strona wygląda tak

Kod
<html>
<head>
    <title> Losowy obrazek </title>
    <script type="text/javascript">
        
        var images = new Array();
        
        <?php
            
            $dir = 'img/';
            $array = Array();
            
            if($dh = opendir($dir))
            {
                $i=0;
                
                while(($pozycja = readdir($dh)) != FALSE)
                    if(!is_dir($pozycja))
                    {
                        echo "images[$i] = $pozycja;\n        ";
                        $array[$i++] = $pozycja;
                    }
                
                closedir($dh);
            }
            echo "\n";

        ?>
        
        function other()
        {
            document.getElementById("img").setAttribute("src", images[Math.floor(Math.random()*images.length)]);
        }
        
    </script>
</head>

<body>

        <input type="button" onClick="other();" value="Losuj inny" />
        <br /><br />
        <?php echo '<img src="'.$dir.$array[rand(0, sizeof($array))].'" id="img" />' ?>
    
</body>
</html>



i chodzi o to że nie wywołuje mi tej funkcji other po kliknięciu w button'a
w firebug'u po kliknięciu występuje błąd other is not defined


co zrobić żeby jak to mój nauczyciel mówi fungowało (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif) , żeby ta funkcja była 'widoczna' ?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
cybertix
post
Post #2





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 28.08.2007

Ostrzeżenie: (0%)
-----


strona jest tutaj http://zsm.slask.pl/ddepta/web/inne/losowy%20obrazek/

i w źródle strony jest nasz skrypt :

  1. <script type="text/javascript">
  2.  
  3. var images = new Array();
  4.  
  5. images[0] = 9.jpg;
  6. images[1] = 8.jpg;
  7. images[2] = 7.jpg;
  8. images[3] = 6.jpg;
  9. images[4] = 5.jpg;
  10. images[5] = 4.jpg;
  11. images[6] = 3.jpg;
  12. images[7] = 2.jpg;
  13. images[8] = 1.jpg;
  14.  
  15.  
  16. function other()
  17. {
  18. document.getElementById("img").setAttribute("src", images[Math.floor(Math.random()*images.length)]);
  19. }
  20.  
  21. </script>


czyli skrypt PHP dobrze działa
Jeżeli by było
  1. <?php
  2. if(is_dir($pozycja))
  3.  
  4. //to w skrypcie by było
  5.        images[0] = .;
  6.        images[1] = ..;
  7. ?>


a mnie przecież interesują pliki(fotki) a nie 'foldery'

i co dalej ?

Ten post edytował cybertix 9.01.2009, 12:49:10
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 12.10.2025 - 18:59