Kevin
Kevin

Reputation: 217

How to pass python variable values to php inside html?

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";
      }

?>

Upvotes: 0

Views: 785

Answers (3)

Matt Williamson
Matt Williamson

Reputation: 40193

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"];

Upvotes: 1

utopMan
utopMan

Reputation: 11

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'];
}

Upvotes: 0

Sammitch
Sammitch

Reputation: 32242

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?

Upvotes: 0

Related Questions