从javascript调用php并从php返回一个数组到Java

从javascript调用php并从php返回一个数组到Java

本文介绍了从javascript调用php并从php返回一个数组到Javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在javascript中编写一个函数,它将调用Getfilename.php并获取在javascript中返回的$ filesArray。

I want to write a function in javascript which will call the Getfilename.php and Get the $filesArray that is return in javascript.

GetFilenme.php是另一个文件我试图从Config.html访问它

GetFilenme.php is another file and I am trying to access this from Config.html

PHP:
Getfilename.php

PHP :Getfilename.php

<?php
$dir = 'uploads/';

$dir = $_REQUEST['dir'] ;

$filesArray = array();
$Counter = 0;
$files = scandir($dir);
foreach ($files as &$file)
{
 if ($file!='.' && $file!='..' )
 {
  $filesArray[$Counter] = $file;
  echo $filesArray[$Counter].'<br>';
  $Counter = $Counter + 1;

 }
}
return $filesArray;
?>


推荐答案

这是假设你下载并包含jQuery javascript库:

This is assuming you download and include the jQuery javascript library:

$(function() {
    $.get('getfilename.php', { dir : 'path/to/dir' }, function(data) {
        // you should now have a json encoded PHP array
        $.each(data, function(key, val) {
            alert('index ' + key + ' points to file ' + val);
        });
    }, 'json');
});

这应该是你的PHP(虽然非常不安全):

This should be your PHP (although very insecure):

<?php
$dir = $_REQUEST['dir'] ;

$filesArray = array();
$Counter = 0;
$files = scandir($dir);

foreach ($files as &$file) {
    if ($file!='.' && $file!='..' ) {
        $filesArray[$Counter] = $file;
        echo $filesArray[$Counter].'';
        $Counter++;
    }
}

echo json_encode($filesArray);
?>

这篇关于从javascript调用php并从php返回一个数组到Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 02:07