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
Rafal Filipek
post
Post #2





Grupa: Zarejestrowani
Postów: 274
Pomógł: 54
Dołączył: 2.05.2006
Skąd: Nadarzyn

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


mi tam takie szybkie sprawdzenie działa
  1. <title> Losowy obrazek </title>
  2. <script type="text/javascript">
  3. var images = new Array();
  4. images[0] = '1.png';
  5. images[1] = '2.png';
  6. images[2] = '3.png';
  7. function other(){
  8. document.getElementById("img").setAttribute("src", images[Math.floor(Math.random()*images.length-1)]);
  9. }
  10. </script>
  11. </head>
  12. <input type="button" onClick="other();" value="Losuj inny" />
  13. <br /><br />
  14. <?php echo '<img src="1.png" id="img" />' ?>
  15. </body>
  16. </html>
Go to the top of the page
+Quote Post
cybertix
post
Post #3





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

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


Cytat(Rafal Filipek @ 8.01.2009, 22:31:19 ) *
mi tam takie szybkie sprawdzenie działa
  1. <title> Losowy obrazek </title>
  2. <script type="text/javascript">
  3. var images = new Array();
  4. images[0] = '1.png';
  5. images[1] = '2.png';
  6. images[2] = '3.png';
  7. function other(){
  8. document.getElementById("img").setAttribute("src", images[Math.floor(Math.random()*images.length-1)]);
  9. }
  10. </script>
  11. </head>
  12. <input type="button" onClick="other();" value="Losuj inny" />
  13. <br /><br />
  14. <?php echo '<img src="1.png" id="img" />' ?>
  15. </body>
  16. </html>



na początku też miałem takie coś i działało (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
tylko nie chciałbym na sztywno samemu tworzyć tą tablice images
chce żeby zrobił to PHP
bo jak bede chciał mieć jakąś nową fotke to bede musiał znowu dopisywać do skryptu images[x] = 'jakas nazwa';
przy paru fotkach nawet może tak być
tylko jak będzie dużo fotek to lepiej żeby PHP mi to zrobił

sprawdzałem całość bez pętli while w skrypcie PHP
czyli

Kod
<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)]);
            alert('działa');

        }
        
    </script>



reszta strony jest ta sama

i funkcja jest wywoływana
tylko czemu z tym while nie działa (IMG:http://forum.php.pl/style_emoticons/default/sciana.gif) ?
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: 16.10.2025 - 09:22