Click here to Skip to main content
14,636,537 members
Rate this:
Please Sign up or sign in to vote.
See more:
HI,

I want to access php function from javascript. this php function returns an array. I have to save it into a variable in javascript for further processing...
Kindly help me and guide me how i do that..

php code is:
<?php

 header("content-type: application/x-javascript");

$dirname="/myphp/MyPP";
$galleryarray = array();

    function returnimages($dirname) {

        $files = array();

        $curimage = 0;


        //valid image extensions

        $pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)";


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


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

                if(eregi($pattern, $file)){

                  galleryarray[$curimage] =  $file';

                    $curimage++;

                }

            }


            closedir($handle);

        }


     return $galleryarray;


    }

    //Define array in JavaScript returnimages()
    //Output the array elements containing the image file names

  returnimages();
?>


html code is:

<script type="text/javascript">

            var galleryarray = returnimages();  /// ==> php function.. here i want to call php function

            var curimg = 0;

            function rotateimages(){


        var imagesDirectory =galleryarray[curimg];

                document.getElementById("slideshow").setAttribute("src", imagesDirectory)

                curimg = (curimg < galleryarray.length - 1) ? curimg + 1 : 0

            }



            window.onload = function(){



             setInterval("rotateimages()", 900)



            }



        </script>
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Javascript runs in the browser on the client machine, PHP code runs on the WWW server. The only way Javascript can communicate with PHP is by making a HTTP request. Look up AJAX[^].
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100