2 回答

TA貢獻1796條經驗 獲得超4個贊
嘗試使用f-string
email = '[email protected]'
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='[email protected]';
? ? ? var password='secret';
? ? ? var data={'key1':133};
? ? ? alert(email);
? ? ? alert(password);
? ? </script>
? </body>
</html>

TA貢獻1826條經驗 獲得超6個贊
您必須在用戶輸入前后添加引號/雙引號:因為最后,如果不添加 '',代碼將像這樣 String([email protected]); 所以你必須添加引號/雙引號
#!/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 protected],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>
""")
添加回答
舉報