Click here to Skip to main content
14,690,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

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:

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

$galleryarray = array();

    function returnimages($dirname) {

        $files = array();

        $curimage = 0;

        //valid image extensions


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

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

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

                  galleryarray[$curimage] =  $file';






     return $galleryarray;


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


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)



1 solution

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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900