Javascript 表单提交后重定向不起作用。 Asp 经典,Javascript

Javascript redirect not functioning after form submit. Asp Classic, Javascript

好吧,经过数小时的研究和尝试,我现在完全被困住了。我为一个网页编写了很多代码,所以我会尽量总结一下。

所以我得到了这个 ASP 经典页面,用户可以在其中使用表单创建票证。 编码页由 3 个主要部分组成:

  1. Javascript 客户端错误处理。
  2. ASP 提交后的经典服务器端错误处理(检查是否 数据库值像电子邮件一样存在)。
  3. 表单提交(发送邮件+插入数据到 数据库)。

表单提交后,有一些 ASP 错误处理。当遇到错误时,将显示一个 Javascript 弹出框并显示错误,并且数据提交和电子邮件将被取消。

表格会自动填充输入的值(使用 asp 代码),以便在出现错误后输入的值会保留在表格中。这一切都有效。

表格代码:

<div align="left"><form name="myForm" method="post" style="display:inline" onsubmit="return(validate());"> 


    <div class="FormTitel">Voor welke afdeling is de ticket bestemd?
    </div>


    <div class="radio">
    <input type="radio" class="radio" name="automatisering" value="Automatisering" checked <% if Request.Form("automatisering") = "Automatisering" then response.write("checked")%>>Automatisering
    <input type="radio" class="radio" name="automatisering" value="Software" <% if Request.Form("automatisering") = "Software" then response.write("checked")%>>Software
    <div id="Error1" style="display:none" color="white"></div>
    </div>


    <div class="FormTitel">Soort ticket
    </div>
     <div class="radio">
    <input type="radio"  class="radio" name="probleem" value="Probleem" <% if Request.Form("probleem") = "Probleem" then response.write("checked")%>>Probleem
    <input type="radio" class="radio" name="probleem" value="Wijziging"<% if Request.Form("probleem") = "Wijziging" then response.write("checked")%>>Wijziging
    <div id="Error2" style="display:none" color="white"></div>
    </div>

    <div class="FormTitel">Uw gegevens
    </div>
    <div>   
    <input type="text" name="Name" class="name" placeholder="Vul hier uw voor- en achternaam in" style="color:#888;" 
    onfocus="inputFocus(this)" onblur="inputBlur(this)" value="<%=Request.Form("Name")%>">
    <div id="Error3" style="display:none" color="white"></div>
    </div>
    <div>       
    <input type="text" name="EMail" class="name" placeholder="Vul hier uw emailadres in" style="color:#888;" 
    onfocus="inputFocus(this)" onblur="inputBlur(this)" value="<%=Request.Form("EMail")%>"/>
    <div id="Error4" style="display:none" color="white"></div>
    </div>

    <input type="submit" class="ticketVerstuur" value="Verstuur ticket" />  
        </form></div>

*错误 ID 是在验证 returns 错误(javascript)时显示的客户端样式框。 *很抱歉没有翻译标题、名字等,但这将是一项相当大的工作;)

表单中的 ASP IF 语句确保在显示错误时返回输入的值并且不会丢失。

好了,现在讲错的部分。

提交后,正在被激活 If Request.ServerVariables("REQUEST_METHOD") = "POST" Then 一些事情正在发生。即,输入值正在为 sql 注入进行参数化,例如,正在服务器端检查一些输入值;数据库中是否存在提交的电子邮件?如果服务器端验证一切正常,则数据将插入数据库并发送电子邮件。

服务器端的错误消息以 javascript 弹出框的形式显示给用户。这行得通。

javascript弹窗代码:

function popup(popup,container) {

        var thisPopup = this;            

        thisPopup.load = function() {            

            container.animate({
                "opacity": "0.3"  
            },250, function() {            
                popup.fadeIn("250");            
            });    

            container.off("click").on("click", function() {
                thisPopup.unload();
            }); 

            $('#closePop').off("click").on("click", function() {            
                thisPopup.unload();             
            });                              
        }       

        thisPopup.unload = function() {            

            popup.fadeOut("250", function(){
                container.animate({
                    "opacity": "1"  
                },250);  
            });
        }
    } 

函数在提交代码中调用如下:new popup($("#popup_box"),$("#container")).load();弹出窗口div放在表单上方,容器包裹在表单周围。 (弹出窗口有效)。

但问题是在服务器端验证一切正常后,数据进入数据库并发送电子邮件,我弹出一个新的 javascript 框,说明一切都成功了。成功后我想清除我的表格(以避免混淆)。

首先,我尝试用 response.redirect ("mypage.asp") 来做到这一点。但是,当我使用它时,javascript 弹出框不会显示。 然后我尝试在弹出框的关闭/卸载功能上使用 window.location.replace("http://whosebug.com");,但这没有效果(数据未被清除)。还尝试在卸载弹出窗口时将 var 设置为 true,然后检查 var 是否设置为 true(然后重定向),但这也不起作用,即使是直接 javascript 重定向,因此,如果没有弹出窗口,提交代码(毕竟成功)似乎由于某种原因不起作用。事实上,只有 alert 似乎有效。这里是调整弹出窗口卸载的例子:

    thisPopup.unload = function() {            

window.location.replace("http://whosebug.com");

popup.fadeOut("250", function(){
                    container.animate({
                        "opacity": "1"  
                    },250);  
                });
            }

那么是什么原因导致了这个问题,我该如何解决呢?我可以想象它需要更多我的代码,所以请不要犹豫,但 post 已经足够大了。

最后但同样重要的是我的代码设置方式的简短摘要:

<html>
<head>
    stylesheet
<title></title>
</head>
<body>
<javascript>
    Pop up box code + validate form code
</javascript>
<form>
</form>
</body>
</html>

<asp server side code (on submit)>
   Some functions (mail - anti injection) + request form values
   Validation server side (with javascript poups)
   Insert data in database
   Sending mail

我认为您的重定向是正确的。要在重定向后显示弹出窗口,请执行以下操作:

response.redirect "mypage.asp?ShowSuccess=true"

然后在 mypage.asp 中使用:

<%
if request("ShowSuccess") = "true" then
    onload = "alert('Success message here!');"
end if
%>
<body onload="<%=onload%>">