Ajax不显示验证消息

时间:2016-05-25 10:58:06

标签: php jquery ajax forms validation

我有一个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>

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

2 个答案:

答案 0 :(得分:1)

使用preventDefault();

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

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

答案 1 :(得分:1)

你必须在你的表格标签中写上onsubmit =“return false”,如