如何将表单提交操作从显示警告弹出窗口更改为重定向到新页面?

How can I change a form submit action from displaying an alert popup to redirecting to a new page?

本文关键字:窗口 重定向 新页面 表单提交 操作 警告 显示      更新时间:2023-09-26

我正在尝试解决别人创建的东西(从来没有一个有趣的前景),并希望消除一个警告弹出窗口,并在表单提交后将用户重定向到另一个页面…我在网上搜索了一下,找到了几个可能的替代方案,但我也不想炸毁这个页面,因为我在PHP故障排除方面没有那么丰富的经验……我猜这是一个非常简单的101级问题,但是提前感谢大家……以下是相关代码:

    jQuery("form#commentform input#submit").click(function(event) {
        event.preventDefault();
        var name = jQuery("form#commentform input#author").val();
        if(name=="") {
            alert("Name cannot be empty!");
            jQuery("form#commentform input#author").focus();
            return false;   
        }
        var email = jQuery("form#commentform input#email").val();
        var pattern = new RegExp(/^(("['w-'s]+")|(['w-]+(?:'.['w-]+)*)|("['w-'s]+")(['w-]+(?:'.['w-]+)*))(@((?:['w-]+'.)*'w['w-]{0,66})'.([a-z]{2,6}(?:'.[a-z]{2})?)$)|(@'[?((25[0-5]'.|2[0-4][0-9]'.|1[0-9]{2}'.|[0-9]{1,2}'.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})'.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})']?$)/i);
        var email_test= pattern.test(email);  
        if (email == "" || email_test == false) {
            alert('Valid email address is required!');
            jQuery("form#commentform input#email").focus();
            return false;
        }
        var phone = jQuery("form#commentform input#url").val();
        var comment = jQuery("form#commentform textarea#comment").val();
        jQuery.get('http://theenergyclub.com/wp-content/themes/EnergyClub/formPost.php', {type:'guestpass', name:name, email:email, phone:phone, comment:comment, rnDnum:Math.random()}, function(data) {
            alert('We have received your request. Thank you!');

您可以尝试替换最后一行

alert('We have received your request. Thank you!');

window.location = "http://example.com/"

您应该能够将alert('We have received your request. Thank you!');更改为window.location = '<URL>';,其中<URL>是您想要重定向用户的URL