将 HTML 页面中的 JavaScript 变量传递到 HTML 表单(在同一页面上)以提交给 Perl CGI

pass a javascript variable within a html page into a html form (on the same page) for submission to perl cgi

本文关键字:HTML 一页 CGI Perl 提交 表单 JavaScript 变量      更新时间:2023-09-26
<script type="text/javascript">
var id = getValue("ID");
document.write(id);
</script>    
<form action="cgi-bin/runalg.cgi" method="post" enctype="multipart/form-data">
<input type="radio" name="genome" value="E.Coli"> <i>E.Coli</i> <input type="radio" name="genome" value="Human"> Human<br> 
<input type="submit" name="submit" value="Submit"/>
<input type="reset" name="reset" value="Clear"/>
</form>
</body>

如何将 JavaScript 变量 (var id( 的值获取到表单中,以便在提交时可以使用 $q<-param("ID") 命令在runalg.cgi中检索它?

你可以直接写:

<script type="text/javascript">
document.write('<input type="hidden" name="ID" value="'+id+'"/>');
</script>

添加隐藏字段。将该字段中的值设置为 Javascript 中变量的值。

<form action="cgi-bin/runalg.cgi" method="post" enctype="multipart/form-data">
[...]
<input type="hidden" name="ID" value="default">
</form>

然后在javascript上:

<script type="text/javascript">
document.forms[0].elements["ID"].value = getValue("ID");
</script>

表单的索引在您的文档中可能会有所不同。