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

时间:2017-11-14 18:51:56

标签: javascript asp-classic

我正在尝试从隐藏的INPUT文本框到另一个页面获取变量。我创建了一个search.asp页面,当我从此页面搜索表单时,单击搜索按钮,然后将使用该特定表单ID打开view.asp页面。在View.asp页面上,我创建了一个链接来打开一个名为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%>$1"></td>
                <td align="center"><input type="radio" id ="Report<%=i%>"  name="Report[<%=i%>]" value="<%=nMenuID%>$<%=nCategoryID%>$2"></td>
                <td align="center"><input type="radio" id ="Report<%=i%>"  name="Report[<%=i%>]" value="<%=nMenuID%>$<%=nCategoryID%>$3"></td>
                <td align="center"><input type="radio" id ="Report<%=i%>"  name="Report[<%=i%>]" value="<%=nMenuID%>$<%=nCategoryID%>$4"></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%>$1"></td>
                <td align="center"><input type="radio" id ="Report<%=i%>"  name="Report[<%=i%>]" value="<%=nMenuID%>$<%=nCategoryID%>$2"></td>
                <td align="center"><input type="radio" id ="Report<%=i%>"  name="Report[<%=i%>]" value="<%=nMenuID%>$<%=nCategoryID%>$3"></td>
                <td align="center"><input type="radio" id ="Report<%=i%>"  name="Report[<%=i%>]" value="<%=nMenuID%>$<%=nCategoryID%>$4"></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>

1 个答案:

答案 0 :(得分:1)

很难理解你想要做什么。

但据我所知,你想打开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>

这是一个自己的链接,因为你有 href =&#34;#&#34; 。但你也试图用这个 onclick =&#34; openview2.asp();&#34; 来调用javascript函数。

您没有名为 openview2.asp() 的功能。这不是它的工作原理。 如果您尝试使用此链接调用 view2.asp 并传递隐藏的输入 idFormName 。如果你只是做一个正确的链接并在网址中传递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 />

我离开隐藏的地方,以防你在代码中的其他地方引用它。

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