Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting html form values to python script

Tags:

python

html

cgi

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.

like image 832
Vittal Cherala Avatar asked Apr 12 '13 07:04

Vittal Cherala


2 Answers

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.

like image 114
scottydelta Avatar answered Sep 23 '22 14:09

scottydelta


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.

like image 38
Xanthoula Atsalaki Avatar answered Sep 22 '22 14:09

Xanthoula Atsalaki