23

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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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.
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.
refer to this tutorialspoint.com/python/python_cgi_programming.htm and there you will find everything you need to know about PYTHON CGI
@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)
@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
|
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.