【问题标题】:Bootstrap validator.js not communicating with my PHPBootstrap validator.js 不与我的 PHP 通信
【发布时间】:2017-03-01 15:30:29
【问题描述】:

我是一名新手编码员,我无法使用 validator.js 将一个简单的表单放在一起

基本上,自从尝试构建表单已停止工作以来,我的猜测是'数据:$('#send-form').serialize(),'没有得到正确的路径或语法,但我可能是错的。

我一加:

$.ajax({
type: "POST",
url: "process.php",
data: $('#send-form').serialize(),
}

警报不会触发!

<form class="contact" name="contact" id="send-form" data-toggle="validator" role="form"  >


  <div class="form-group">
    <label class="subTitle" for="name">NAME</label><br>
    <input type="text" name="name" class="form-control" placeholder="Enter your full name" data-error="Please enter your name" required>
    <div class="help-block with-errors"></div><br>
  </div>

  <div class="form-group">
    <label class="subTitle" for="email">E-MAIL</label><br>
    <input type="email" name="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" class="form-control" placeholder="Enter a valid email address" data-error="Invalid email address" required>
    <div class="help-block with-errors"></div><br>
  </div>

    <div class="form-group">
      <button type="submit" class="subBtn form-control">SUBMIT YOUR ENQUIRY</button>
    </div>

</form> 




    



<script>
$('#send-form').validator().on('submit', function (e) {
    if (e.isDefaultPrevented()) {
        alert('form is not valid');
    } else {
        // everything looks good!
        e.preventDefault();
        alert('form is valid');
        // your ajax
                $.ajax({
                type: "POST",
                url: "process.php",
                data: $('#send-form').serialize(),
                }
});

</script>

【问题讨论】:

  • 可能是因为语法错误?
  • 您在 ajax 调用之后缺少一个 );,而 else 子句缺少一个 } --> $('#send-form').validator().on('submit', function (e) { if (e.isDefaultPrevented()) { alert('form is not valid'); } else { // everything looks good! e.preventDefault(); alert('form is valid'); // your ajax $.ajax({ type: "POST", url: "process.php", data: $('#send-form').serialize() }); } });

标签: javascript php jquery ajax twitter-bootstrap


【解决方案1】:

应该是:

$('#send-form').validator().on('submit', function (e) {
    if (e.isDefaultPrevented()) {
        alert('form is not valid');
    } else {
        // everything looks good!
        e.preventDefault();
        alert('form is valid');
        // your ajax
        $.ajax({
            type: "POST",
            url: "process.php",
            data: $('#send-form').serialize(),
        });
    }
});

(未经测试)

【讨论】:

  • 是的,语法错误。代码现在可以在线运行。非常感谢
猜你喜欢
  • 1970-01-01
  • 2015-05-25
  • 2017-08-16
  • 2015-03-12
  • 1970-01-01
  • 2013-01-29
  • 2012-07-11
  • 2019-04-06
  • 2012-11-28
相关资源
最近更新 更多