简体   繁体   中英

How to get value of javascript variable in python variable in Python-CGI script

I want to take the value of javascript variable into python variable.

This is Python-CGI script which displays a selection box and selected value appears on the page. But I want to take selected value in python variable.

#!/usr/bin/python2.7 -u
import time, sys, os

# Import modules for CGI handling
import cgi, cgitb

sys.stdout.write('Content-Type: text/html;charset=utf-8\n')
print "\n\n"
print "<html>"
print "<body>"

rows = ['1.1.1.1', '2.2.2.2', '3.3.3.3']
print "<script>"
print "function showData() {"
print "var sel = document.getElementById('combo');"
print "var value = sel.value;"
print "var text = sel.options[sel.selectedIndex].text;"
print "document.getElementById('secondP').innerHTML=text;}"
print "</script>"

print '<form name="comboform">'
print '<select name="comboselect" onchange="showData()" id="combo">'
for row in rows:
    print "<option value='%s'>%s</option>"%(row,row)
print "</select>"
print "</form>"

print '<p id="secondP">&nbsp;</p>'

# HERE I WANT TO TAKE VALUE OF SELECTED IP ADDRESS (var value) IN PYTHON VARIABLE
# selected_ip_adr = value #something like this where value has selected ip address
# NEED TO PASS THE SAME TO ANOTHER SCRIPT

sys.stdout.flush()
print "</body>"
print "</html>"

It would be great if someone can suggest a work-around to achieve this.

I was able to resolve it with following code and multiple pages:

# Create instance of FieldStorage
form = cgi.FieldStorage()
op = form.getvalue('combo')

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