简体   繁体   English

如何在Python-CGI脚本中获取python变量中javascript变量的值

[英]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. 我想将javascript变量的值转换为python变量。

This is Python-CGI script which displays a selection box and selected value appears on the page. 这是Python-CGI脚本,它显示一个选择框,并且所选值出现在页面上。 But I want to take selected value in python variable. 但是我想在python变量中采用选定的值。

#!/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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用javascript选择Python-CGI Web应用程序中的所有复选框 - select all checkboxes in Python-CGI Web application with javascript 如何将python变量发送到javascript? python是cgi脚本吗? - How do I send a python variable over to javascript? The python is a cgi script? 将javascript变量传递给python程序(已导入cgi) - Passing javascript variable to a python program(cgi imported) 如何在CGI脚本中的JavaScript和Python之间进行通信 - How to communicate between JavaScript and Python in a CGI script 如何获取 JavaScript function 到 Python 变量的返回值 - How to get the return value of a JavaScript function to a Python Variable 如何获取 bash 脚本中的 JavaScript 变量值? - How to get the JavaScript variable value in the bash script? 如何使用 Selenium 和 Python 从 Javascript 获取变量值 - How to get variable value from Javascript using Selenium with Python 如何将python变量的值(字符串)分配为JavaScript变量中的值 - How to assign a python variable's value(string) as the value in JavaScript variable 如何将HTML变量的值发送到HTML中的JavaScript - How to send the value of a Python variable to JavaScript in HTML 如何将JavaScript变量值传递给python变量(烧瓶) - how to pass JavaScript variable value into python variable(flask)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM