重定向具有多个提交输入的表单

时间:2022-09-25 16:25:57

I have a form which has 3 separate input with a type of "submit".

我有一个表单,其中有3个单独的输入和一个“提交”类型。

The first two inputs output a message once they are clicked, but the final one is supposed to redirect to another page however it is not doing this. I think this is because the form is submitting and refreshing the page before it gets to the JavaScript for redirection.

前两个输入一旦被单击就会输出一条消息,但最后一个输入应该重定向到另一个页面但是它没有这样做。我认为这是因为表单在访问JavaScript以进行重定向之前提交并刷新页面。

Here is my code:

这是我的代码:

FORM.php

<form class="form" id="form" method="POST" enctype="application/x-www-form-urlencoded">
    <br>
         <input type="email" name="email" id="email" maxlength="80" value="<?php echo $email ?>" placeholder="Enter Your Email" /><br /><br>
        <input id="button4" type="submit" value="Get Security Question" name="submit2" style="cursor:pointer;"/><br> 
            <br><input type="password" name="securitya"id="securitya" maxlength="20" value="<?php echo $securitya ?>" placeholder="Enter Your Security Answer" /> <br />
        <br>
    <input id="button3"type="submit" value="Check Answer" name="submit" style="cursor:pointer;"/>
    <br>
        <br><input type="password" name="newpassword" id="newpassword" maxlength="20" placeholder="Enter Your New Password" /> <br />

    <br><input type="password" name="confirmpassword" id="confirmpassword" maxlength="20" placeholder="Re-Enter Your New Password" /> <br />

<br>
    <input id="button2" type="submit" value="Change Password" disabled="disabled" name="submit" style="cursor:pointer;"/>

JavaScript

jQuery(function(){
        $("#button2").click(function(){                                         
        $(".error").hide();
        var hasError = false;
        var passwordVal = $("#newpassword").val();
        var checkVal = $("#confirmpassword").val();
        if (passwordVal == '') {
            $("#newpassword").after('<span class="error" >Please enter a password.</span>');
            hasError = true;
        } else if (checkVal == '') {
            $("#confirmpassword").after('<span class="error">Please re-enter your password.</span>');
            hasError = true;
        } else if (passwordVal != checkVal ) {   
            $("#confirmpassword").after('<span id = "pass" class="error">Passwords do not match.</span>');
            hasError = true;
        }
        if(hasError == true) { return false; }
        else {

            $("#button2").after('<span class="error">Passwords accepted.</span>');

            window.location.href='adminsignin.php';
            }

    }); 
});

What is weird about this is the message 'Passwords accepted' appears on screen for a split second, but there is no redirection to 'adminsignin.php'

奇怪的是,屏幕上出现了“密码已接受”一瞬间,但没有重定向到'adminsignin.php'

Can anyone help?

有人可以帮忙吗?

1 个解决方案

#1


2  

In your javascript, pass the event into the click handler, and preventDefault().

在您的javascript中,将事件传递给click处理程序,并使用preventDefault()。

$("#butotn2").click(function(e) {
    e.preventDefault();
   ...
}

the submit button has default behavior, so you'll want to prevent its default behavior so that you can perform your operations and then redirect with your window.location.href.

提交按钮具有默认行为,因此您需要阻止其默认行为,以便您可以执行操作,然后使用window.location.href重定向。

please try changing to document.location = 'your URL';

请尝试更改为document.location ='您的网址';

#1


2  

In your javascript, pass the event into the click handler, and preventDefault().

在您的javascript中,将事件传递给click处理程序,并使用preventDefault()。

$("#butotn2").click(function(e) {
    e.preventDefault();
   ...
}

the submit button has default behavior, so you'll want to prevent its default behavior so that you can perform your operations and then redirect with your window.location.href.

提交按钮具有默认行为,因此您需要阻止其默认行为,以便您可以执行操作,然后使用window.location.href重定向。

please try changing to document.location = 'your URL';

请尝试更改为document.location ='您的网址';