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

93
Visualizações
Pass PHP result via Ajax and use as variable In Javascript

I'm a novice in Javascript and Ajax, but with some help of the internet I got halfway there. But now I'm stuck.

I'm sending some HTML form data to a PHP script using Ajax. The PHP file should return two values. When I put console.log(data); in the success: function(data) block, I can see an array of the two values in Console.

But how can I now use these two values in two separate variables?

Btw, the Ajax call is in the main html file, but I would need these two variables in a functions.js file, where all my other Javascript lives.

EDIT:

The console says [80,120].

A snippet from the bottom of index.html

<script  src="assets/function.js"></script>

<script type="text/javascript">
    $('#calculate').click(function() {
        var val1 = $('#number').val();
    $.ajax({
        type: 'POST',
        url: 'calc.php',
        data: { number: val1 },
        success: function(data) {
            console.log(data); 
        }
    });
});     
</script>

calc.php

...some calculations which end in...
<?php
$Var1 = 80;
$Var2 = 120;
?>

function.js

...
var value1 = ??? //here I'd need the value of $Var1
var value2 = ??? //here I'd need the value of $Var1
...
further calculations
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