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

192
Visualizações
Executing a PHP page from AJAX and waiting for the results

I'm fairly new to Javascript. I'd like to use Javascript to execute a PHP script on the server and capture the result (which is a few items of CSV text), and then use that result to update my webpage.

I already have a generic Javascript function to execute a PHP page, which is this:

function executePhpPage(url)
{
    // executes a PHP page
    try
    {
        httpxml = new XMLHttpRequest();
        httpxml.onreadystatechange = 
            function()
            {
                if (this.readyState == 4 && this.status == 200) 
                {
                    return httpxml.responseText;
                }
            }
        httpxml.open("GET", url, true);
        httpxml.send(null);
    }
    catch (e)
    {
        alert(e.message);
    }
}

However, control is returned to the caller when the thread of execution drops out of the bottom of the function, before any results are returned. Therefore, I've tried modifying it to this:

function sleep(ms)
{
    // see https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep
    return new Promise(resolve => setTimeout(resolve, ms));
}

function executePhpPage(url) // returns the results of the page
{
    // executes a PHP page and returns the results
    try
    {
        httpxml = new XMLHttpRequest();
        httpxml.open("GET", url, true);
        httpxml.send(null);
        while (!(this.readyState == 4 && this.status == 200) )
        {
            sleep(10);
        }
        return httpxml.responseText;
        
    }
    catch (e)
    {
        alert(e.message);
    }
}

The call to the function is:

var host = window.location.protocol + "//" + window.location.host;
var url = host + "/AJAX_NewsletterSubscribe.php?email=" + cEmail + "&download=1";
alert(url);
results = executePhpPage(url);
alert(results);

'''alert(results)''' is never reached, the webpage becomes inactive and I have to kill it from the task manager. Presumably, it is looping round the sleep repeatedly.

How can I wait until the results come in before returning execution from executePhpPage and return the resulting data?

I'd prefer answers in straight Javascript rather than JQuery and or anything if possible. Thanks.

about 4 years ago · Juan Pablo Isaza
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