1

Take for example a simple php form such as ,

<html>  
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
<input type="submit">
</form>

</body>
</html>

Is it possible to POST whatever value that a user input into a python script namely app.py which has a variable named as "name" eg:-

name = #This Variable needs to be populated with the value the user inputs to the form
print(name + " "+ "is my name")

Apologies about the vague question but I am quite new to programming and would be great if someone can at least point me in the right direction if this is possible. Another doubt here is on clicking the submit button how can I trigger the python script.

dev_tech
  • 47
  • 7

1 Answers1

0

Your form is already sending a POST request, so it makes more sense to post it directly to your Python application, if it's running on a web server. If for whatever reason you want to pass an HTTP request on to another web application, you can use PHP CURL extension. Take a look at this PHP + curl, HTTP POST sample code? and this https://www.php.net/manual/en/curl.examples-basic.php

If the Python app is not running on a web server, you can call it as a command line script from your PHP application. It would look something like this:

<?php
// welcome.php

$name = $_POST['name'];
$pythonScriptCall = 'python3 app.py name=' . $name;
$pythonScriptResult = '';
exec($pythonScriptCall, $pythonScriptResult);
echo $pythonScriptResult;
Nenad Mitic
  • 577
  • 4
  • 12