Posting html form values to python script

100,499

Solution 1

in the form action form action="", put the location of your cgi script and the value of the textbox will be passed to the cgi script. eg.

<form name="search" action="/cgi-bin/test.py" method="get">
Search: <input type="text" name="searchbox">
<input type="submit" value="Submit">
</form> 

in your test.py

import cgi
form = cgi.FieldStorage()
searchterm =  form.getvalue('searchbox')

thus you will get the key word entered in search text box in searchterm variable in python.

Solution 2

I had the problem with getting the code in browser. Installed python and xampp. I put the .py script in cgi-bin and I called it from an html page like that

<form name="input" action="../cgi-bin/name_of_script.py" method="get">

Put the html page in htdocs of xampp. In the httd.conf find the line

AddHandler cgi-script .cgi .pl .asp 

and change it to

AddHandler cgi-script .cgi .pl .asp .py

In the script add the version of the python you have for example I added

#!C:\Python27\python.exe

because I have installed python27 in the above directory.

Also if you print something in python put this line on top of the script

print "Content-Type: text/html; charset=utf-8\n\n";

The above script of the searchbox it worked fine for me. My operating system is Windows really torchered me, I searched and searched for the above solution.

Share:
100,499
Vittal Cherala
Author by

Vittal Cherala

Updated on July 09, 2022

Comments

  • Vittal Cherala
    Vittal Cherala almost 2 years

    I have created html form with text box and button

    enter ur search keyword

    My requirement is to pass the text box value in to my test.py code, is there any way to do it. Please suggest me how do it.

  • Vittal Cherala
    Vittal Cherala about 11 years
    Thanks for your reply, I have followed your procedure but i am getting entire code of test.py in the browser when i click the submit button.
  • Vittal Cherala
    Vittal Cherala about 11 years
    Thanks it worked but i have another requirement. When i run my test.py script it is creating one .jpg file. So how can i display my .jpg file on my browser.
  • scottydelta
    scottydelta about 11 years
    refer to this tutorialspoint.com/python/python_cgi_programming.htm and there you will find everything you need to know about PYTHON CGI
  • eiselems
    eiselems about 9 years
    @VittalCherala Create another question if you have other issues (besides you have to specify the content-type in your return: e.g. print("Content-type: image/jpg") and then give your image data)
  • Yuvraj Gupta
    Yuvraj Gupta over 8 years
    @VittalCherala I also get test.py code when I click submit button, can you tell what should i do to get output instead of code?
  • scottydelta
    scottydelta over 8 years
    @YuvrajGupta: You are getting the code because the web server doesnt know that it has to execute the python code so instead its just giving the python file on the browser. To get the code to run, follow instructions on configuring web server as per guidelines on this doc: tutorialspoint.com/python/python_cgi_programming.htm
  • Yuvraj Gupta
    Yuvraj Gupta over 8 years
    @scottydelta I did the same, unfortunately getting the same error.