简体   繁体   English

表单提交成功后重定向

[英]Redirection after successful form submit

I have a form which should submit data after pressing the submit button.我有一个表单,应在按下提交按钮后提交数据。 After tagging a few input fields as required the form always shows me when there is no input in the required field after pressing the submit button - so far, so good.根据需要标记一些输入字段后,当按下提交按钮后所需字段中没有输入时,表单总是向我显示 - 到目前为止,一切都很好。

What I would like to realize is that there is a redirection to another page if the submission was successful.我想意识到的是,如果提交成功,则会重定向到另一个页面。 If there are some empty required fields the form should show me, without redirecting me to another page.如果有一些空的必填字段,表单应该显示给我,而不会将我重定向到另一个页面。

By now I have the following code:到目前为止,我有以下代码:

Submit button:提交按钮:

<div class="form-group">
     <div class="col-sm-offset-2 col-sm-10">
    <button type="submit" name="submityes" id="submityes" class="btn btn-danger">Submit</button>
      </div>
  </div>

Also I have the following js function to submit the form and to redirect me to another page:此外,我还有以下 js 函数来提交表单并将我重定向到另一个页面:

$('document').ready(function () {
    "use strict";
    $(function () {
        $('#submityes').click(function () {
            $.ajax({
                type: "POST",
                /* url: "process.php", //process to mail
                data: $('form.contact').serialize(), */
                success: function (msg) {
                    window.location.replace("/submit_resolved.php");
                },
                error: function () {
                    alert("error");
                }
            });
        });
    });
});

The problem I have right now is that I will always be redirected to the "submit_resolved.php" page, whether all required fields are complete or not.我现在遇到的问题是,无论所有必填字段是否完整,我都会被重定向到“submit_resolved.php”页面。

How can I solve this problem?我怎么解决这个问题? I only want to be redirected when all required fields are not empty.我只想在所有必填字段都不为空时重定向。

You should bind to the submit event, not click event:您应该绑定到submit事件,而不是click事件:

UPDATED TO MATCH THE COMMENTS更新以匹配评论

$(function () {

    var submityesClicked;

    //catch the click to buttons
    $('#submityes').click(function () {
        submityesClicked = true;
    });
    $('#submitno').click(function () {
        submityesClicked = false;
    });

    $('#webform').submit(function (e) {
        e.preventDefault();//prevent the default action

        $.ajax({
            type: "POST",
            /*url: "process.php", //process to mail
             data: $('form.contact').serialize(),*/
            success: function (msg) {
                window.location.replace(submityesClicked ? "/submit_resolved_yes.php" : "/submit_resolved_no.php");
            },
            error: function () {
                alert("error");
            }
        });
    });
});

The submit event is triggered only if the form is valid.只有当表单有效时才会触发submit事件。

Note that the submit event is triggered by the form but the click event is triggered by the input element.请注意, submit事件是由表单触发的,而click事件是由input元素触发的。

Do redirection on complete .complete上做重定向。 Not on success不是成功

$('document').ready(function () {
    "use strict";
    $(function () {
        $('#submityes').click(function () {
            $.ajax({
                type: "POST",
                /* url: "process.php", //process to mail
                data: $('form.contact').serialize(), */
                success: function (msg) {
                    //window.location.replace("/submit_resolved.php");
                },
                complete: function () {
                    window.location.replace("/submit_resolved.php");
                },
                error: function () {
                    alert("error");
                }
            });
        });
    });
});

I assume you are validating form in process.php so, you have to return error if validation fail from process.php like this.我假设您正在 process.php 中验证表单,因此,如果像这样从 process.php 验证失败,您必须返回错误。

header('HTTP/1.1 500 Internal Server Booboo');
header('Content-Type: application/json; charset=UTF-8');
die(json_encode(array('message' => 'ERROR', 'code' => 1337)));

check this link: Return errors from PHP run via.检查此链接: 从 PHP run via 返回错误。 AJAX? 阿贾克斯?

Hope this may be helpful to you.希望这可能对您有所帮助。

The simplest thing you can do is to add "required" attribute to you input elements.Example:您可以做的最简单的事情是向输入元素添加“必需”属性。示例:

<form action="/action_page.php">
  Username: <input type="text" name="usrname" required>
  <input type="submit">
</form> 

It's a HTML5 attribute, so no JavaScript required.这是一个 HTML5 属性,因此不需要 JavaScript。 And it is supported by all major browsers.并且所有主流浏览器都支持它。 Check this link:检查此链接:

http://caniuse.com/#search=required http://caniuse.com/#search=required

Anyway, you shouldn't rely just on front-end verification.无论如何,您不应该仅仅依赖前端验证。 Check those inputs on back-end, too.也检查后端的这些输入。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
     <div class="col-sm-offset-2 col-sm-10">
        <form action="">
           Username: <input type="text" id="usrname" required>
          <button type="button" name="submityes" 
           id="submityes" class="btn btn-danger">Submit</button>
       </form> 
  </div>

function isValid(){
  var usrname = $("#usrname").val();
    if(usrname == ""){
      return false;
    }
  return true;
}
$(function () {
    $('#submityes').submit(function () {
    if(isValid() == true){
        $.ajax({
            type: "POST",
           /*url: "process.php", //process to mail
            data: $('form.contact').serialize(),*/
            success: function (msg) {
                alert("success");
                window.location.replace("/submit_resolved.php");
            },
        });
        }else{
        alert("error");
        }
    });
});

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

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