0

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

2
Contributors
1
Reply
25
Views
3 Months
Discussion Span
Last Post by broj1
0

Not sure what exactly is the expected workflow but few notes anyway:

The PHP code in the beginning of the test.php should probably only execute after form submission or when the $_POST values are set so maybe perform a check or you might get errors/warnings:

if(isset($_POST['submit'])) {
    $user = $_POST['user'];
    echo 'User: ';
    echo $user. '<br><br>';
    ...
}

It is still not clear whether this code is necessary at all since your form redirects to the `welcome.php` page.

If you want to catch the output of your python script into the $output variable you have to set that as a parameter, something like

exec("python D:/text.py", $output);

Now you should have the output stored in the $output variable as an array. Maybe some other PHP external command function may be more appropriate (such as passthru - see https://secure.php.net/manual/en/function.passthru.php).

If you are on Linux make sure you have ownership and access permissions of the text.py script sorted right so it actually runs.

When inserting user supplied values into html code (whenever you use $_POST values in your html) you should sanitize them - use the htmlspecialchars function so possible harmful <script> or <iframe> tags get converted to &lt;script&gt; or &lt;iframe&gt; safe variants.

Edited by broj1

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.