在javascript中调用php函数并将返回值保存在变量中 [英] call php function in javascript and save returning value in variable

查看:93
本文介绍了在javascript中调用php函数并将返回值保存在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,

我想从javascript访问php函数.这个PHP函数返回一个数组.我必须将其保存到javascript中的变量中以进行进一步处理...
请帮助我,并指导我如何做..

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:

<?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代码是:



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>

推荐答案

dirname = " ;
dirname="/myphp/MyPP";


galleryarray = array(); 函数returnimages(
galleryarray = array(); function returnimages(


dirname){
dirname) {


这篇关于在javascript中调用php函数并将返回值保存在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆