Click here to Skip to main content
12,897,945 members (86,424 online)
Rate this:
Please Sign up or sign in to vote.
See more: Javascript HTML PHP

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)
Posted 30-Aug-12 23:25pm

1 solution

Rate this: bad
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 31 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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