I am making a webpage wherein I am going to ask for an input through an html form using php, and then call a python script after clicking "submit (flames)" to run and process the Flames game. Can someone help me on how to execute a python script through a php button? Thanks.
My (text.py) contains the code of the flames game and it looks like this:
import sys
print ("This is the name of the script: " + sys.argv[0])
print ("The arguments are: " + str(sys.argv))
# step 1: Input User and Crush
user = sys.argv[1]
crush = sys.argv[2]
# Convert string into list of char
c_user = list(user)
c_crush = list(crush)
# step 2: Cross Out Matching letters
# copy c_crush to a temporary variable since we will be removing items here and we need the original
# temp_crush = c_crush merely creates a new reference from c_crush to temp_crush so we added [:] for actual copying
temp_crush = c_crush[:]
for i in c_user:
while True:
if i in c_crush:
print "Removing " + i + " in " + str(c_crush)
c_crush.remove(i)
else:
break
for j in temp_crush:
while True:
if j in c_user:
print "Removing " + j + " in " + str(c_user)
c_user.remove(j)
else:
break
# check the remaining letters
print c_user
print c_crush
# step 3: count remaining letters(n)
matchCount = len(c_user) + len(c_crush)
# step 4: iterate n to FLAMES
# Modulo(%) takes the remainder when dividing integers as output. We use this since when we count after 6(S), we go back to 0(F).
output = matchCount % 6
# step 5: print output
if output == 0:
print user + " and " + crush + " are (F)riends."
elif output == 1:
print user + " and " + crush + " are (L)overs."
elif output == 2:
print user + " and " + crush + " are (A)cquaintances."
elif output == 3:
print user + " and " + crush + " are (M)arried."
elif output == 4:
print user + " and " + crush + " are (E)nemies."
elif output == 5:
print user + " and " + crush + " are (S)iblings."`
my form called (test.php) which is the homepage looks like:
<?php
$user = $_POST['user'];
echo 'User: ';
echo $user. '<br><br>';
$crush = $_POST['crush'];
echo 'Crush: ';
echo $crush. '<br><br>';
exec("python D:/text.py");
print ($output)
?>
<html>
<head>
<title>TEST</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>
<form action="welcome.php" method="post">
Name number one: <input type="text" name="user"><br>
Name number two: <input type="text" name="crush"><br>
<input type="submit" value="FLAMES">
</form>
</div>
</body>
</html>
and my (welcome.php) which is the result page looks like:
<?php
$user = $_POST['user'];
echo 'User: ';
echo $user. '<br><br>';
$crush = $_POST['crush'];
echo 'Crush: ';
echo $crush. '<br><br>';
exec("python D:/text.py");
print ($output)
?>
The output I'm getting is just like this. I think there's something wrong on how I am calling the python script.
User: ashley
Crush: zac
Notice: Undefined variable: output in C:\xampp\htdocs\TEST\welcome.php on line 11