【问题标题】:Ajax not displaying validation messagesAjax 不显示验证消息
【发布时间】:2016-05-25 10:58:05
【问题描述】:

我有一个 PHP 联系表单,我正在尝试对其实施 Ajax 以使其更加用户友好,因为联系表单是使用 javascript/jQuery 的弹出式表单,因此每次提交表单时都会刷新页面,您必须返回表单才能查看验证消息。

你能检查一下我在这里遗漏了什么吗?我已经用了一段时间了,但它仍然不起作用。如果您希望我添加 PHP 脚本,请告诉我。

Ajax 代码:

jQuery(document).ready(function ($) {

        $("#ajax-contact-form").submit(function () {
            var str = $(this).serialize();

            $.ajax({
                type: "POST",
                url: "demoform.php",
                data: str,

                success: function (msg) {
                    if (msg == 'OK') {
                        result = '<div class="notification_ok">Your message has been sent. Thank you!</div>';
                        $("#fields").hide();
                    } else {
                        result = msg;
                    }

                    $("#note").html(result);
                }
            });

            return false;
        });
    });

HTML 表单

<center>
<p>Please complete the form below <br />to request a <br/> <span id="free_demo">FREE DEMO</span></p>

<div id="note"></div>
    <div id="fields">
<form id="ajax-contact-form" enctype="multipart/form-data">

<div><input type="text" placeholder="Name" name="fullname" id="vpbfullname"     value="<?php echo strip_tags($_POST["fullname"]); ?>" class="vpb_input_fields">    <?php echo $submission_status1; ?></div>
<div><input type="text" placeholder="E-Mail" name="email"  id="email" value="<?php echo strip_tags($_POST["email"]); ?>" class="vpb_input_fields"><?php echo $submission_status2; ?></div>
<div><input type="text" placeholder="Company" name="company" id="company" value="<?php echo strip_tags($_POST["company"]); ?>" class="vpb_input_fields"><?php echo $submission_status3; ?></div>
<div><input type="text" placeholder="Telephone" name="phone" id="telephone" value="<?php echo strip_tags($_POST["phone"]); ?>" class="vpb_input_fields"><?php echo $submission_status4; ?></div>

<div class="vpb_captcha_wrappers"><input type="text" placeholder="Security  Code" id="vpb_captcha_code" name="vpb_captcha_code" class="vpb_input_fields">    </div>
<br/><br/><br/>
<a href="javascript:void(0);" id="refresh"  onClick="vpb_refresh_aptcha();">Refresh Security Code</a></font>
<br/><br/>
<div class="vpb_captcha_wrapper"><img src="vasplusCaptcha.php?rand=<?php   echo rand(); ?>"     id='captchaimg' ></div><br clear="all"><br/>

<br clear="all"> 

<div><?php echo $submission_status; ?></div><!-- Display success or error messages -->
<div>&nbsp;</div>

<input type="hidden" id="submitted" name="submitted" value="1">
<input type="submit" class="vpb_general_button"  value="Submit"></form>    </div>

<br clear="all">
</center>

任何帮助都会很棒..谢谢

【问题讨论】:

    标签: php jquery ajax forms validation


    【解决方案1】:

    使用preventDefault();

    $("#ajax-contact-form").submit(function (e) {
       e.preventDefault();
       //....
    };
    

    这会阻止表单提交,但会发生 ajax 调用。

    【讨论】:

    • 感谢它发送电子邮件,并且表单使用 Ajax 可以正常工作。唯一不起作用的是使用 Ajax 的验证消息
    • 不工作是什么意思? $("#note").html(result); 应该将 msgdiv 推入。在失败的情况下尝试console.log(msg);。你确定你回应了demoform.php 的内容吗?
    • 天啊!!!你钉它...它的工作原理!!!!我没有从 PHP 表单中回应任何内容。我多么愚蠢..非常感谢您的帮助:)
    【解决方案2】:

    你必须在你的表单标签中写上 onsubmit="return false" 像

    【讨论】:

    • 谢谢您,我尝试了您的建议,但仍然没有显示验证消息。
    • 感谢 Sunil,但它现在已解决,除非您仍想查看 PHP 脚本?正如“@lolka_bolka”所建议的那样,PHP 脚本的问题在于它没有回显任何内容。向 PHP 添加了 echo,验证现在可以工作了。再次感谢您的时间和支持。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 2017-12-26
    • 1970-01-01
    相关资源
    最近更新 更多