22

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.

warren
  • 32,620
  • 21
  • 85
  • 124
Vittal Cherala
  • 447
  • 1
  • 6
  • 14

2 Answers2

33

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.

Community
  • 1
  • 1
scottydelta
  • 1,786
  • 4
  • 19
  • 39
  • 8
    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 Apr 12 '13 at 10:10
  • 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. – Vittal Cherala Apr 12 '13 at 12:10
  • 3
    refer to this http://www.tutorialspoint.com/python/python_cgi_programming.htm and there you will find everything you need to know about PYTHON CGI – scottydelta Apr 12 '13 at 12:51
  • @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) – eiselems Feb 25 '15 at 07:03
  • @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? – Yuvraj Gupta Sep 28 '15 at 10:20
  • 3
    @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: http://www.tutorialspoint.com/python/python_cgi_programming.htm – scottydelta Sep 29 '15 at 21:52
  • @scottydelta I did the same, unfortunately getting the same error. – Yuvraj Gupta Sep 30 '15 at 07:15
5

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.

Tunaki
  • 132,869
  • 46
  • 340
  • 423