如何获取从输入框传递到另一个页面的变量

How to get variable passed from input box to another page

我正在尝试从隐藏的 INPUT 文本框获取一个变量到另一个页面。我创建了一个 search.asp 页面,当我从此页面搜索表单时,单击搜索按钮,然后 view.asp 页面将打开该特定表单 ID。在 View.asp 页面上,我创建了一个 link 来打开一个名为 view2.asp 的新页面,该页面保持相同的表单 ID。但它没有用。你能帮忙吗?谢谢

view.asp代码

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Menus Search</title>

</head>
<body>

<style>
table, td, th {
    border: 1px solid #ddd;
}
th, td {
    padding: 3px;
}
</style>
</head>
<body>
<!--#include file="openConn.asp" -->   

<%
SET objRS = Server.CreateObject("ADODB.Recordset")
SET objRS1 = Server.CreateObject("ADODB.Recordset")
SET objRS2 = Server.CreateObject("ADODB.Recordset")
SET objRS3 = Server.CreateObject("ADODB.Recordset")

If Request.Form("FormSource") = "SubmitForm" Then

    For j = 1 to Request.Form("txtCount")-1
        strReport   = Request.Form("Report[" & j &"]")

        strValue    = split(strReport,"$")
        sMenuID     = strValue(0)
        sCategoryID = strValue(1)
        sStatus     = strValue(2)

'response.write strReport  & "::" & sMenuID & "::"  & sCategoryID & "::"  & sStatus & "<br>"
'response.end       
        sFormID     = Request.Form("FormID")

        sSQL =  "INSERT INTO Report (FormID, MenuID, CategoryID, Status) VALUES " &_
                "('" & sFormID & "', '" & sMenuID & "', '" & sCategoryID & "', '" & sStatus & "');" 
            objConn.Execute(sSQL)
    Next
End If
%>

<form action="" method="post" id="newMenu" name="frmReport"  onsubmit="return validateForm(this)">
<p>
<%
'''' RR added today
nFormID= Request("FormID") ''Request.Form("SearchObj")
response.write "FormID: " & nFormID & "<br>"

sSQL2 = "SELECT * FROM Form WHERE Formid = " & nFormID
''response.write "SQL:::: " & sSQL2 & "<br>"

    objRS2.Open sSQL2, objConn
    'response.Write sSQL2
    Do Until objRS2.EOF
        sFormName = objRS2("Form_Name")
%>
<h2><%= objRS2("Form_Name") %></h2>
<p><a href="#" onclick="openview2.asp();"/>Click me</a></p>
    <input type="hidden" id="idFormName" name="FormID" size="40" maxlength="50" value="<%= objRS2("FormID") %>"><br />
<%
    objRS2.MoveNext
    Loop
    objRS2.Close
%>

<p>
<table>
<thead bgcolor="#336666" style="color:#FFFFFF">
    <tr>

        <td>Trainer Name:</td>
        <td>Pass</td>
        <td>Fail</td>
        <td>NA</td>
        <td>Not taken</td>
    </tr>
</thead>
<%
currMenu = ""
sSQL =  "SELECT MenuID, Menu_Name FROM Menu where MENUID in (SELECT MENUID FROM Category where formid=" & nFormID & ")"

    objRS.Open sSQL, objConn
    i=1
    While Not objRS.EOF
        nMenuID     = objRS("MenuID")
        sMenuName   = objRS("Menu_Name")
        If currMenu <> sMenuName Then
            currMenu = sMenuName
            %>
            <tr>
                <th bgcolor="#CCCCCC"><%= sMenuName %></th>
            </tr>
                <input type="hidden" name="MenuID" value="<%=nMenuID%>">
        <% 
        End If 
        sSQL3 = "SELECT Categoryid, Category_Name FROM Category WHERE MenuID = " & nMenuID & " and FormID=" &  nFormID
            Set objRS3 = Server.CreateObject("ADODB.Recordset")
            objRS3.Open sSQL3, objConn
            While Not objRS3.EOF
                nCategoryID     = objRS3("Categoryid")
                sCategoryName   = objRS3("Category_Name")
        %>
            <tr>
                <td><%= sCategoryName %></td>
                <input type="hidden" name="CategoryID" value="<%=nCategoryID%>">
                <td align="center"><input type="radio" id ="Report<%=i%>"  name="Report[<%=i%>]" value="<%=nMenuID%>$<%=nCategoryID%>"></td>
                <td align="center"><input type="radio" id ="Report<%=i%>"  name="Report[<%=i%>]" value="<%=nMenuID%>$<%=nCategoryID%>"></td>
                <td align="center"><input type="radio" id ="Report<%=i%>"  name="Report[<%=i%>]" value="<%=nMenuID%>$<%=nCategoryID%>"></td>
                <td align="center"><input type="radio" id ="Report<%=i%>"  name="Report[<%=i%>]" value="<%=nMenuID%>$<%=nCategoryID%>"></td>
            </tr>
            <%
            objRS3.MoveNext
            i = i + 1
            Wend
            objRS3.Close
        objRS.MoveNext
    Wend
    objRS.Close
%>
</table>
</p>

<p>
<input type="hidden" name="txtCount" value="<%= i %>">
<input type="button" value="Create Menu" onclick="openwin();" />
<input type="hidden" name="FormSource" value="SubmitForm">
<input type="button" value="View2" onclick="openview2();" />
<input type="submit" value="Update">
<a href="search.asp">Go To Search</a>
</p>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript" ></script>   
<script type="text/javascript">
function validateForm(daForm) {
nCount = document.frmReport.txtCount.value;

    // check all rb radio buttons
    for (var i = 1; i < nCount; i++) {
        if (! getCheckedRadioValue(daForm["Report"+i])) {
            alert ("Please select a value for option " + i)
            return false
        }
    }
    // add other checks here...
    return true
}
function getCheckedRadioValue(radio) {
    for (var i=0; i < radio.length; i++) {
        if (radio[i].checked) return radio[i].value
    }
    return false
}
function openwin()
{
    //alert($('#idFormName').val());
    //window.location.href = "Create.asp?FormID=" + $('#idFormName').val();

    window.open("Create.asp?FormID=" + $('#idFormName').val(), "Create New Menu", "menubar=0,width=700,height=450");

}
function openView2()
{
alert($('#idFormName').val());
    window.open("view2.asp?FormID=" + $('#idFormName').val(), "Create New Menu", "menubar=0,width=700,height=450");
}
</script>
</body>
</html>

view2.asp

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Menus Search</title>

</head>
<body>

<style>
table, td, th {
    border: 1px solid #ddd;
}
th, td {
    padding: 3px;
}
</style>
</head>
<body>
<!--#include file="openConn.asp" -->   

<%
SET objRS = Server.CreateObject("ADODB.Recordset")
SET objRS1 = Server.CreateObject("ADODB.Recordset")
SET objRS2 = Server.CreateObject("ADODB.Recordset")
SET objRS3 = Server.CreateObject("ADODB.Recordset")

If Request("FormID")="" then
    iFormID =0
Else
    iFormID = Request("FormID") ''Request.Form("FormID")
End If

If Request.Form("FormSource") = "SubmitForm" Then

    For j = 1 to Request.Form("txtCount")-1
        strReport   = Request.Form("Report[" & j &"]")

        strValue    = split(strReport,"$")
        sMenuID     = strValue(0)
        sCategoryID = strValue(1)
        sStatus     = strValue(2)

'response.write strReport  & "::" & sMenuID & "::"  & sCategoryID & "::"  & sStatus & "<br>"
'response.end       
        sFormID     = Request.Form("FormID")

        sSQL =  "INSERT INTO Report (FormID, MenuID, CategoryID, Status) VALUES " &_
                "('" & sFormID & "', '" & sMenuID & "', '" & sCategoryID & "', '" & sStatus & "');" 
            objConn.Execute(sSQL)
    Next
End If
%>

<form action="" method="post" id="newMenu" name="frmReport"  onsubmit="return validateForm(this)">
<p>
<%
'''' RR added today
nFormID= Request("FormID") ''Request.Form("SearchObj")
response.write "FormID: " & nFormID & "<br>"

sSQL2 = "SELECT * FROM Form WHERE Formid = " & nFormID
''response.write "SQL:::: " & sSQL2 & "<br>"

    objRS2.Open sSQL2, objConn
    'response.Write sSQL2
    Do Until objRS2.EOF
        sFormName = objRS2("Form_Name")
%>
<h2><%= objRS2("Form_Name") %></h2>
    <input type="hidden" id="idFormName" name="FormID" size="40" maxlength="50" value="<%= objRS2("FormID") %>"><br />
<%
    objRS2.MoveNext
    Loop
    objRS2.Close
%>

<p>
<table>
<thead bgcolor="#336666" style="color:#FFFFFF">
    <tr>

        <td>Trainer Name:</td>
        <td>Pass</td>
        <td>Fail</td>
        <td>NA</td>
        <td>Not taken</td>
    </tr>
</thead>
<%
currMenu = ""
sSQL =  "SELECT MenuID, Menu_Name FROM Menu where MENUID in (SELECT MENUID FROM Category where formid=" & nFormID & ")"

    objRS.Open sSQL, objConn
    i=1
    While Not objRS.EOF
        nMenuID     = objRS("MenuID")
        sMenuName   = objRS("Menu_Name")
        If currMenu <> sMenuName Then
            currMenu = sMenuName
            %>
            <tr>
                <th bgcolor="#CCCCCC"><%= sMenuName %></th>
            </tr>
                <input type="hidden" name="MenuID" value="<%=nMenuID%>">
        <% 
        End If 
        sSQL3 = "SELECT Categoryid, Category_Name FROM Category WHERE MenuID = " & nMenuID & " and FormID=" &  nFormID
            Set objRS3 = Server.CreateObject("ADODB.Recordset")
            objRS3.Open sSQL3, objConn
            While Not objRS3.EOF
                nCategoryID     = objRS3("Categoryid")
                sCategoryName   = objRS3("Category_Name")
        %>
            <tr>
                <td><%= sCategoryName %></td>
                <input type="hidden" name="CategoryID" value="<%=nCategoryID%>">
                <td align="center"><input type="radio" id ="Report<%=i%>"  name="Report[<%=i%>]" value="<%=nMenuID%>$<%=nCategoryID%>"></td>
                <td align="center"><input type="radio" id ="Report<%=i%>"  name="Report[<%=i%>]" value="<%=nMenuID%>$<%=nCategoryID%>"></td>
                <td align="center"><input type="radio" id ="Report<%=i%>"  name="Report[<%=i%>]" value="<%=nMenuID%>$<%=nCategoryID%>"></td>
                <td align="center"><input type="radio" id ="Report<%=i%>"  name="Report[<%=i%>]" value="<%=nMenuID%>$<%=nCategoryID%>"></td>
            </tr>
            <%
            objRS3.MoveNext
            i = i + 1
            Wend
            objRS3.Close
        objRS.MoveNext
    Wend
    objRS.Close
%>
</table>
</p>

<p>
<input type="hidden" name="txtCount" value="<%= i %>">
<input type="button" value="Create Menu" onclick="openwin();" />
<input type="hidden" name="FormSource" value="SubmitForm">

<input type="submit" value="Update">
<a href="search.asp">Go To Search</a>
</p>
</form>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" type="text/javascript" ></script>   
<script type="text/javascript">
function validateForm(daForm) {
nCount = document.frmReport.txtCount.value;

    // check all rb radio buttons
    for (var i = 1; i < nCount; i++) {
        if (! getCheckedRadioValue(daForm["Report"+i])) {
            alert ("Please select a value for option " + i)
            return false
        }
    }
    // add other checks here...
    return true
}
function getCheckedRadioValue(radio) {
    for (var i=0; i < radio.length; i++) {
        if (radio[i].checked) return radio[i].value
    }
    return false
}
</script>
</body>
</html>

很难理解您要做什么。

但据我了解,您想打开 view2.asp 并能够从那里获得 view.asp 的值。

正如我所看到的,你用这行 view2.asp 打开

<input type="button" value="View2" onclick="openview2();" />

你想要这个隐藏的输入

<input type="hidden" id="idFormName" name="FormID" size="40" maxlength="50" value="<%= objRS2('FormID') %>">

如果这就是您想要做的。那你的命名就错了javascript。您可能复制并忘记更改名称。

看这里,你尝试打开的地方 view2.asp

function openwin()
{
    window.openView2("view2.asp?FormID=" + $('#idFormName').val(), "Create New Menu", "menubar=0,width=700,height=450");
}

需要将openwin()改为openview2()

编辑:

我刚刚也看到你有这个奇怪的代码

<a href="#" onclick="openview2.asp();"/>Click me</a>

它本身就是一个 link,因为你有 href="#"。但是您还尝试使用此 onclick="openview2.asp();".

调用 javascript 函数

您没有名为 openview2.asp() 的函数。这不是它的工作原理。 如果您尝试使用此 link 调用 view2.asp 并传递隐藏的输入 idFormName。那么最好只做一个正确的 link 并在 url 中传递 ID。 我必须说这是一个非常糟糕的解决方案,因为这里没有安全保障。但是您提供的代码表明您只是在执行一个函数而忽略了任何安全方面。

所以改变这个

<h2><%= objRS2("Form_Name") %></h2>
<p><a href="#" onclick="openview2.asp();"/>Click me</a></p>
    <input type="hidden" id="idFormName" name="FormID" size="40" maxlength="50" value="<%= objRS2("FormID") %>"><br />

为此

<h2><%= objRS2("Form_Name") %></h2>
<p><a href="view2.asp?idFormName=<%= objRS2("FormID") %>" />Click me</a></p>
    <input type="hidden" id="idFormName" name="FormID" value="<%= objRS2("FormID") %>"><br />

我将隐藏内容留在那里,以防您在代码中的其他地方引用它。

祝你好运,祝你有愉快的一天!