Hello there.
I am creating a web page using php wherein a user will login then after clicking the button, it will run the python script I made. Is it possible for the python script to run and get the user input value then save it on a variable on php?

I have a php named index.php for the login page:

        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <form action="result.php">
           <label for="username">Username: </label>
           <input required type="email" name="username" placeholder="juan_delacruz@email.com" id="username"/><br><br>
           <label for="password">Password: </label>
           <input required type="password" name="password" id="password"/><br><br>
             Select Site: <select name="site">
           <label for="clustername">Cluster Name: </label>
           <input required type="text" name="clustername" id="clustername"/><br><br>
           <input type="submit" value="LOGIN"/>

I have a php named index.php for the login page:

And here's the code for the result page named result.php:

$username = $_GET['username'];
echo 'Username: ';
echo $username. '<br><br>';

$password = $_GET['password'];
echo 'Password: ';
echo $password. '<br><br>';

$site = $_GET['site'];
echo 'Site: ';
echo $site. '<br><br>';

$clustername = $_GET['clustername'];
echo 'Cluster Name: ';
echo $clustername. '<br><br>';

#calling python script
$pyscript = 'D:/test.py';

$python = 'C:\Python27.exe';

$cmd = "$python $pyscript";
exec("$cmd", $result);
echo $result;


the code of the python script that I am trying to call is this:

#step 1: Input User and Crush
user= raw_input("User number 1: ")
crush = raw_input("User number 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)

for j in temp_crush:
  while True:
    if j in c_user:
      print "Removing " + j + " in " + str(c_user)

#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."

When I execute it, the output just looks like this:

Username: abc@abc.com

Password: a

Site: NED

Cluster Name: a

Notice: Array to string conversion in C:\xampp\htdocs\TEST\result.php on line 25

Can somebody help me to solve this? Thanks.

2 Weeks
Discussion Span
Last Post by Kim Gabrielle

Do a print_r($result) on line 25. What do you get?

Edited by Gurjit_2


Username: abc@abc.com

Password: a

Site: NED

Cluster Name: a

Array ( )

that's the output I got. But what I'm trying to do is have a value input from the user because as you can see, the python script will ask for 2 names so that in can execute the result of what their relationship is, according to the game called flames.


The array is empty ,meaning the two names are not being passed. I suspect the bug can be found between line 19 and 24 of result.php


is there a way that I can call and output the python script first, so I can gather the user input for the two names then it will be passed as a variable on php for the result.php to be shown?

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