Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
<?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:

XML
<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

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[^].
 
Share this answer
 

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