【发布时间】:2021-10-09 20:14:51
【问题描述】:
我是 jquery 和 php 的新手。我有一个html页面,需要通过jquery在一个php文件中使用一些函数,然后用html打印结果。
我要做的是计算 3 个不同目录中的文件数,然后在 3 个不同的 div 中打印它们的结果。当我在 php 文件中只编写一个函数,并在 jquery 中通过load 调用它时,它工作正常。但我不知道如何在同一个 php 文件中将此方法用于 3 个不同的函数。你能帮帮我吗?
HTML:
<div class="result1"></div>
<div class="result2"></div>
<div class="result3"></div>
<script>window.onload = stats()</script>
JS:
function stats() {
$('.result1').load("counter.php"); //I don't know what to add here to get the result of only the function a1 in counter.php
$('.result2').load("counter.php"); //and for a2
$('.result3').load("counter.php"); //and for a3
}
PHP:
<?php
function a1() {
$i = 0;
$dir = '../folder1/';
if ($handle = opendir($dir)) {while (($file = readdir($handle)) !== false){if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) $i++;}}
echo "$i";
}
function a2() {
$i = 0;
$dir = '../folder2/';
if ($handle = opendir($dir)) {while (($file = readdir($handle)) !== false){if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) $i++;}}
echo "$i";
}
function a3() {
$i = 0;
$dir = '../folder3/';
if ($handle = opendir($dir)) {while (($file = readdir($handle)) !== false){if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) $i++;}}
echo "$i";
}
?>
【问题讨论】:
标签: javascript php html jquery