• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

214
Views
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.

about 3 years ago · Juan Pablo Isaza
1 answers
Answer question

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.

about 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error