0

I have a simple python code

path1 = //path1/
path2 = //path2/

write_html = """

<form name="input" action="copy_file.php" method="get">

"""
Outfile.write(write_html)

Now copy_file.php copies files from one folder to another. I want the python path1 and path2 variable values to be passed to php script. How can I do that? Also instead of calling a php script, how can I place the php code in action attribute.

Php code

<?php
      $file = $argv[1]
      $newfile = $argv[1]

      if (!copy($file, $newfile)) {
      echo "failed to copy $file...\n";
      }

?>

1
  • Why are you sending a request to a PHP script? Why not copy the file with Python since you're already using it? Commented Dec 20, 2013 at 21:54

3 Answers 3

1
write_html = """

<form name="input" action="copy_file.php" method="get">

<input type="hidden" name="path1" value="{0}" />
<input type="hidden" name="path2" value="{1}" />

<input type="button" name="button" value="onClick="copyfile('{0}', '{1}')"/> 

<script> function moveFile(path1, path2){ ...} </script>

""".format(path1, path2)

Then in copy_file.php add

$path1 = $_GET["path1"];
$path2 = $_GET["path2"];
Sign up to request clarification or add additional context in comments.

3 Comments

Can it be arrranged like js - <input type="button" name="button" value="onClick="copyfile('%s', '%s')"/> <script> function moveFile(path1, path2){ ...} </script> in php.
Sure, you are just generating php from python.
Updated my answer to reflect it.
0

I want the python path1 and path2 variable values to be passed to php script.

Doable:

write_html = """

<form name="input" action="copy_file.php" method="get">
<input type="hidden" name="path1" value="%s" />
<input type="hidden" name="path2" value="%s" />

""" % (path1, path2)

My python is a bit rusty, but that should work.

instead of calling a php script, how can I place the php code in action attribute.

What? No. Are you insane?

Edit: wait, are you just trying to make an HTTP request against a PHP script from within Python?

Comments

0

Passing arguments to your PHP script the way you do looks like for me to be done like:

in python file

path1 = '//path1/'
path2 = '//path2/'

write_html = """

<form name="input" action="copy_file.php" method="get">
    <input type="hidden" name="path1" value="%s"/>
    <input type="hidden" name="path2" value="%s"/>
</form>
""" % (path1, path2)
Outfile.write(write_html)

this will vrite your variables to html ( you may escape them if special chars )

and on php side :

if isset($_POST['path1'] && $_POST['path2']) {
    $path1 = $_POST['path1'];
    $path2 = $_POST['path2'];
}

2 Comments

how can I have this on onclick() event.
using a form is simpler in your case. as params values are written into your forms inputs, you just need to add this input to yor form to enable data post <input type="submit" /> this will produce a clickable button that triggers the HTTP POST requests to your server target : copy_file.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.