Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
Jquery: Get the functions from a php file

I'm new to jquery and php both. I have an html page and need to use some functions in a php file via jquery, and then print the results in html.

What I'm trying to do is counting the number of files in 3 different directories, and then print their results in 3 different divs. When I write only one function in the php file, and call it via load in jquery, it works fine. But I don't know how to use this method for 3 different functions in the same php file. Could you please help me?

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";
}

?>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Add a URL parameter that indicates which function to call. Then check that in the PHP.

JS:

function stats() {
    $('.result1').load("counter.php?action=a1");
    $('.result2').load("counter.php?action=a2");
    $('.result3').load("counter.php?action=a3");
}

PHP:

<?php

switch ($_GET['action']) {
case 'a1':
    a1();
    break;
case 'a2':
    a2();
    break;
case 'a3':
    a3();
    break;
}

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";
}

?>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda