javascript - 查询阻止从 php 提交响应时的表单

标签 javascript php jquery

如果用户收到电子邮件存在于数据库中的消息,我将尝试停止或禁用提交按钮。

我尝试在两者之间加入 if,但没有成功。没有 jquery 大师。

有人说我应该使用 e.preventDefault();但我该把它放在哪里呢?到处都试过了,但要么脚本不起作用,要么用户无论如何都可以注册。

也许最好验证 php 中的所有内容?只是认为使用验证插件时 jQuery 更好。

有什么线索吗?谢谢=)

有这个脚本:

Jquery:

    $("#email").keyup(function (e){
    clearTimeout(x_timer);
    var e_mail = $(this).val();
            if (e_mail.length > 4) {
        x_timer = setTimeout(function(){
            check_email_ajax(e_mail);
        }, 1000);
    }
}); 

    function check_email_ajax(e_mail){
        $("#email-result").html('<img src="../cms/assets/images/loading.gif" width="20px" height="20px" />');
        $.post('ajax/register.ajax.php', {'email':e_mail}, function(data) {
          $("#email-result").html(data);
        });
    }

PHP:

    if(isset($_POST["email"])) {

        if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND     strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
        die();
    }

$email = filter_var($_POST["email"], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);

$statement = $link->prepare("SELECT email FROM FF_Users WHERE email=?");
$statement->bind_param('s', $email);
$statement->execute();
$statement->bind_result($email);
if($statement->fetch()){
    die('<i class="fa fa-times-circle" style="color:red;" aria-hidden="true"></i> <small>(taken)</small>');
}else{
    die('<i class="fa fa-check" style="color:green;" aria-hidden="true"></i> <small>(Yeey :-) )</small>');
}}

最佳答案

您可以使用 button type="button" 完美地在没有提交按钮的情况下工作。我认为您正在寻找这样的东西:

function check_email_ajax(e_mail){
        $("#email-result").html('<img src="../cms/assets/images/loading.gif" width="20px" height="20px" />');
        $.post('ajax/register.ajax.php', {'email':e_mail}, function(data) {
          $("#email-result").html(data);
          var result= $("i").attr("id")
          if (result==red){
             //If this is the result you stay on the page and user see the red message :)
          }
          if (result==green){
            //If result is green I imagine you want to go where the action in form tag says, so delete action from the form, you can simply redirect where you want:
            url = "your-file.php"
            $(location).attr("href", url)
          }
        });
    }

为图标添加 ID:

if($statement->fetch()){
    die('<i class="fa fa-times-circle" id="red" style="color:red;" aria-hidden="true"></i> <small>(taken)</small>');
}else{
    die('<i class="fa fa-check" id ="green" style="color:green;" aria-hidden="true"></i> <small>(Yeey :-) )</small>');
}}

希望对您有帮助。

关于javascript - 查询阻止从 php 提交响应时的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41496563/

相关文章:

javascript - 如何在 Javascript 中使用滚动事件(无 Jquery)

php - 两个表的逻辑问题

php - 在 BigCommerce 上应用外部运费

jquery - 为什么这只能在 Firefox 中使用?

javascript - jQuery 选择器上下文与最外层元素不匹配

javascript - 如何在angularjs中获取周明智的开始和结束日期

javascript - 正则表达式:没有全局标志的重复捕获组

javascript - 使用 eval() 获取对导入的引用

javascript - 从 ajax 调用时 HTML 未显示在 div 中

javascript - Jasmine 的 toThrow 匹配器是否需要将参数包装在匿名函数中?