简体   繁体   中英

Posting html form values to python script

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM