2 回答
TA贡献1796条经验 获得超4个赞
尝试使用f-string
email = 'jack@jack.com'
password = 'secret'
val1 = 133
html = f"""
<!DOCTYPE html>
<html>
<body>
<script>
var email='{email.strip()}';
var password='{password.strip()}';
var data={{'key1':{val1}}};
alert(email);
alert(password);
</script>
</body>
</html>
"""
print(html)
输出
<!DOCTYPE html>
<html>
<body>
<script>
var email='jack@jack.com';
var password='secret';
var data={'key1':133};
alert(email);
alert(password);
</script>
</body>
</html>
TA贡献1826条经验 获得超6个赞
您必须在用户输入前后添加引号/双引号:因为最后,如果不添加 '',代码将像这样 String(email@gmail.com); 所以你必须添加引号/双引号
#!/usr/bin/python3
import cgi
import cgitb; cgitb.enable()
print ("Content-type:text/html\n\n")
form = cgi.FieldStorage()
email=str(form.getvalue("email")[0])
password=str(form.getvalue("password")[0])
print (email,password) # email@email.com,123456
print ("""
<!DOCTYPE html>
<html>
<body>
<script>
var email=String('"""+str(email.strip())+"""');
var password=String('"""+str(password.strip())+"""');
alert(email);
alert(password);
</script>
</body>
</html>
""")
添加回答
举报