简体   繁体   English

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

[英]Javascript redirect not functioning after form submit. Asp Classic, Javascript

Alright, after hours and hours of research and trying things I am completely stuck at the moment. 好吧,经过数小时的研究和尝试,我此刻完全陷入困境。 I coded quite a lot for a web page so I'll try to summarize it as good as possible. 我为一个网页编写了很多代码,所以我将尽可能地对其进行总结。

So I've got this ASP CLASSIC page where a user can create a ticket using a form. 因此,我有了这个ASP CLASSIC页面,用户可以在其中使用表单创建故障单。 The coded page consists out of 3 main parts: 编码页面由3个主要部分组成:

  1. Javascript client side error handling. Javascript客户端错误处理。
  2. ASP CLASSIC server side error handling after submission (checking if database values exists like email). 提交后的ASP CLASSIC服务器端错误处理(检查是否存在数据库值,如电子邮件)。
  3. The form submitting (sending an email + inserting the data into the database). 表单提交(发送电子邮件+将数据插入数据库)。

After the form is submitted there is some ASP error handling. 提交表单后,将进行一些ASP错误处理。 When a error is encountered a Javascript popup box will be shown with the error and the data submission and email is being cancelled. 遇到错误时,将显示一个带有错误的Javascript弹出框,并且将取消数据提交和电子邮件。

The form is auto filled with the inputted values (with asp code) so that after a error the inputted values are kept in the form. 表单将自动用输入的值(带有asp代码)填充,以便在出现错误后将输入的值保留在表单中。 This all works. 所有这一切。

The form code: 表单代码:

<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>

*The error id's are client side styling boxes that are being showed when a Validation returns false (with javascript). *错误ID是验证返回false(使用javascript)时显示的客户端样式框。 *Sorry for not translating the title's, names, etc. but that would be quite some work;) *很抱歉没有翻译标题,名称等,但这将是很多工作;)

The ASP IF statements in the form make sure that the inputted values are returned and not lost when a error is being showed. 表单中的ASP IF语句可确保在显示错误时返回输入值,并且不会丢失输入值。

Alright now for the part where it goes wrong. 现在,对于发生错误的部分,请继续。

After submission, which is being activated by If Request.ServerVariables("REQUEST_METHOD") = "POST" Then some stuff is happening. 提交后,如果If Request.ServerVariables("REQUEST_METHOD") = "POST" Then激活,则正在发生某些事情。 Namely, the input values are being parameterized for sql injection, some input values are being checked on the server side, for example; 也就是说,正在为sql注入对输入值进行参数化,例如,正在服务器端检查某些输入值; does the submitted email exists in the database? 提交的电子邮件是否存在于数据库中? and if the server side validation is all fine then the data is being inserted in the database and a email is being send. 如果服务器端验证一切正常,则将数据插入数据库中并发送电子邮件。

The error messages on the server side are being shown to the user as javascript popup boxes. 服务器端的错误消息将作为JavaScript弹出框显示给用户。 This works. 这可行。

The javascript popup code: 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);  
            });
        }
    } 

The function is called in the submit code like so: new popup($("#popup_box"),$("#container")).load(); 在提交代码中调用该函数,如下所示: new popup($("#popup_box"),$("#container")).load(); The popup div is put above the form and the container is wrapped around the form. 弹出div放在表单上方,容器包装在表单周围。 (popup works). (弹出窗口)。

The problem though is that after the server side validation is all good, so the data goes into the database and the email is send, I popup a new javascript box saying that the everything was successful. 问题是,在服务器端验证一切正常之后,因此数据进入了数据库并发送了电子邮件,我弹出一个新的javascript框,说一切都成功了。 When it is successful I want to clear my form (to avoid confusion). 成功后,我想清除表格(避免混淆)。

First I try'd to do this with a response.redirect ("mypage.asp") . 首先,我尝试使用response.redirect ("mypage.asp") Though, when I use this the javascript popup box wont show. 不过,当我使用此选项时,不会显示javascript弹出框。 Then I try'd to use a window.location.replace("http://stackoverflow.com"); 然后,我尝试使用window.location.replace("http://stackoverflow.com"); on my close / unload function of the popup box, this has no effect though (data is not being cleared). 在我的弹出框的关闭/卸载功能上,这没有任何作用(数据未清除)。 Also try'd it with setting a var to true when the popup is unloaded and then later checking the var if it is set to true (then redirect) but this also doesn't work, even a direct javascript redirect, so without the popup, in the submission code (after all is successful) doesn't seem to work for some reason. 还可以尝试在卸载弹出窗口时将var设置为true,然后再检查var是否设置为true(然后重定向),但这也不起作用,即使是直接的javascript重定向也是如此,因此没有弹出窗口,由于某种原因,提交代码中的代码(毕竟成功了)似乎不起作用。 In matter of fact only alert seems to work. 实际上,只有警报似乎起作用。 Here the example of the adjusted popup unload: 以下是调整后的弹出窗口卸载的示例:

    thisPopup.unload = function() {            

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

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

So what causes this problem and how can I fix it? 那么是什么导致了这个问题,我该如何解决呢? I can imagine it requires a bit more of my code so don't hesitate to ask for it, but the post is big enough as it is. 我可以想象它需要更多我的代码,所以不要犹豫地要求它,但是这篇文章足够大了。

Last but not least a short summary of how my code is setup: 最后但并非最不重要的简短摘要是如何设置我的代码:

<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

I think you were on the right track with the redirect. 我认为您在重定向方面正处于正确的轨道上。 To get a pop-up to show after a redirect, do something like this: 要使重定向后显示弹出窗口,请执行以下操作:

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

Then in mypage.asp use this: 然后在mypage.asp中使用以下命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM