Skip to content
Advertisement

How to pass a variable in PHP from a Python file?

I’m trying to call a Python script in a PHP file and pass a variable value from that script after pressing the submit button. I have it:

index.php

JavaScript

test.py

JavaScript

Both files are in the same folder. Why is it not working? I’d like to do this without Flask / Django.

Advertisement

Answer

You can’t declare a variable in Python and use it directly in PHP after execution. Try to output your variable in Python:

JavaScript

Now you can get the result of your script by the return value of the exec function:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement