简体   繁体   English

如何停止AJAX功能提交?

[英]How to stop AJAX function submit?

I'm working on an AJAX function for my site. 我正在为我的网站开发AJAX函数。 I'm facing three problems. 我面临三个问题。

  1. My page has 2 JavaScript pop-up error message instead of 1 with the same message. 我的页面有2条JavaScript弹出错误消息,而不是1条相同的消息。
  2. Combine the two isset post in the PHP. 在PHP中合并两个isset帖子。 It doesn't works for me 这对我不起作用
  3. If the message == success , how can it submit with the action in the form else it will stay in the same page. 如果message == success ,它将如何以动作形式提交,否则它将停留在同一页面中。

My HTML: 我的HTML:

<head>

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<body>
<form method="POST" id="contactForm" action="www.alphaexile.com/method?=>
<input type="text" name="email" id="email"></input>
<input type="submit" name="submit1"></input>

</form>
<script type="text/javascript">
$('#contactForm').submit(function(e){
    e.preventDefault();
    var email = $('#email').val();
    $.ajax({
        type: 'POST',
        dataType: 'JSON',
        url: 'check.php',
        data: {email: email},
        success: function(data){
            if(data.status == 'success') {
                alert('The e-mail address entered is correct.');
            } else {
                alert('The e-mail address entered is Incorrect.');
            }
        }
    });
});
</script>
</body>
</head>

My PHP: 我的PHP:

    <?php

if (!empty($_POST['email'] || ($_POST['email']="Enter Your Email Here")) {
    $status = 'success';
} else {
    $status = 'failed';
}

echo json_encode(array('status' => $status));

?>

Simple way would be to register your execute your ajax code on click of the submit button, then submit the form in the submit action. 简单的方法是在单击“提交”按钮后注册执行ajax代码,然后在“提交”操作中提交表单。

$('#contactForm [type="submit"]').click(function(e){
  e.preventDefault();
  var email = $('#email').val();
  $.ajax({
    type: 'POST',
    dataType: 'JSON',
    url: 'check.php',
    data: {email: email},
    success: function(data){
        if(data.status == 'success') {
            alert('The e-mail address entered is correct.');
            $('#contactForm').submit()
        } else {
            alert('The e-mail address entered is Incorrect.');
        }
    }
  });

})

For 3). 对于3)。

First you need to change the name of the submit button form submit to something else then 首先,您需要将提交按钮表单的名称更改为其他,然后再submit

$('#contactForm').submit(function (e) {
    e.preventDefault();
    var email = $('#email').val();
    var frm = this;
    $.ajax({
        type: 'POST',
        dataType: 'JSON',
        url: 'check.php',
        data: {
            email: email
        },
        success: function (data) {
            if (data.status == 'success') {
                alert('The e-mail address entered is correct.');
                frm.submit();
            } else {
                alert('The e-mail address entered is Incorrect.');
            }
        }
    });
});

For 2) - Not a PHP guy(so I don't know whether you can use $_POST twice, but if you don't want to use it twice). 对于2)-不是PHP专家(所以我不知道您是否可以两次使用$ _POST,但是如果您不想两次使用它)。 But it looks like the empty() call is not properly closed 但是看来empty()调用未正确关闭

<?php

$email = $_POST['email'];
if (empty($email) || $email="Enter Your Email Here") {
    $status = 'failed';
} else {
    $status = 'success';
}

echo json_encode(array('status' => $status));

?>

This should solve all your issue. 这应该解决您所有的问题。

You define a global variable, you set it when ajax is finished. 您定义一个全局变量,在ajax完成时进行设置。 First set it to false, meaning you still need to do some ajax call. 首先将其设置为false,这意味着您仍然需要进行一些Ajax调用。 But once the ajax call success you resubmit the form. 但是,一旦ajax调用成功,您就重新提交表单。

This time since you set the checker to true. 这次是因为您将检查器设置为true。 which means the ajax call is done, it will not perform that call again and instead it will just submit the form. 这意味着ajax调用已完成,它将不会再次执行该调用,而只会提交表单。

var formSubmit = false;   // define a global variable
    $('#contactForm').submit(function(e){
        if(!formSubmit){
            e.preventDefault();
            var emailParam = $('#email').val();  // change you variable name, it's confusing to read.
            $.ajax({
                type: 'POST',
                dataType: 'JSON',
                url: 'check.php',
                data: {email: emailParam},
                success: function(data){
                    if(data.status == 'success') {
                        alert('The e-mail address entered is correct.');.
                         formSubmit = true;  // set the global variable to true.
                         $('#contactForm').trigger('submit')  //resubmit the form
                    } else {
                        alert('The e-mail address entered is Incorrect.');
                    }
                }
            });
        } else {
           formSubmit = false;
        }
    });

For the PHP part you can do it like this 对于PHP部分,您可以这样做

if (isset($_POST['email']) && $_POST['email'] != 'Enter Your Email Here') {
    $status = 'success';
} else {
    $status = 'failed';
}

echo json_encode(array('status' => $status));
die();

Check if email parameter is set in $_POST and check the value. 检查$_POST是否设置了email参数,并检查该值。

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

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