Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

64
Vistas
Can I set a javascript variable to be a string that contains the output from php code?

I am trying to set a javascript variable to be equal to the output of php code:

var name = '<?php global $email; echo json_encode(passthru("python Backend/User/getName.py $email"));?>';

When this runs, it returns the correct value, but it also attaches a null value to it:

var name = 'name
null';

This causes the code to take the value as null and not as the name returned.

7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As stated, passthru returns null on success and false on failure.

What you are looking to get is the content of the file so a simple way to do it is to just use output buffering.

You can have a simple function to return the value of your script like so:

<?php
function getPassthruValue(string $command)
{
    ob_start();
    passthru($command);
    $returnValue = ob_get_contents();
    ob_end_clean();
    return $returnValue;
}
global $email;
$value = getPassthruValue("python Backend/User/getName.py $email");
?>
<script>
    var passthruValue = "<?= $value ?>"
</script>

<?= === <?php echo just incase you didn't know what the shorthand annotation means.

This will grab the value of your python script and then set it to a js value.

This will return an empty string on failure so you may need to put some exception handling in depending on your needs.

7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.