Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

89
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda