weixin_33736649 2015-09-01 11:39 采纳率: 0%
浏览 29

Ajax表单未提交结果

I have a signup page. I do a JQuery validation and then make an AJAX call to regForm.php to upload the new user if all of the fields are correct - pretty basic.

My problem

The validation is working but, when user registers, NOTHING HAPPENS. I get no success or error message, nothing written to the database, and my console on Chrome shows nothing. So I am pretty much stuck.

I'm sure it is just a small problem.... Perhaps something I'm missing?

HTML

 <div id="ajaxMsgDiv"></div>
        <form name="registration-form" id="regForm" method="post" action="regForm.php">
         <span id="sname-info" style="color:red"></span>
        Name: <br /><input type="text" id="sname" name="fName" onfocus="this.value='';" /><br />
        <span id="ssurname-info" style="color:red"></span>
        Surname:<br /> <input type="text" id="ssurname" name="fSurname" onfocus="this.value='';" /> <br />
        <span id="sspword-info" style="color:red"></span>
        Password:<br /><input type="text" id="spword" name="fPword" onfocus="this.value='';" /><br />
        <span id="sconfirmpword-info" style="color:red"></span>
        Confirm Pword:<br /><input type="text" id="sconfirmpword" name="pwordConfirm" onfocus="this.value='';" /><br />
         <span id="sEmail" style="color:red"></span>
        Email:<br /><input type="text" id="sEmail" name="sEmail" onfocus="this.value='';" /><br />
         <span id="sPhone-info" style="color:red"></span>
        Phone:<br /><input type="text" name="sPhone" id="sPhone" onfocus="this.value='';" /><br />
        Male<input type="radio" value="male"  name="sex">Female<input type="radio" value="female" name="sex"><br />
        <input type="submit" onclick="" value="Register" name="register" class="buttono" />
        </form>

JQUERY

    <script type="text/javascript">
$(function () {
    var form = $('#regForm');
    var formMessages = $('#ajaxMsgDiv');

    // Set up an event listener for the contact form.
    $(form).submit(function (e) {
        // Stop the browser from submitting the form.
        e.preventDefault();

        //do the validation here
        if (!validateReg()) {
            return;
        }

        // Serialize the form data.
        var formData = $(form).serialize();

        // Submit the form using AJAX.
        $.ajax({
            type: 'POST',
            url: $(form).attr('action'),
            data: formData
        }).done(function (response) {
            // Make sure that the formMessages div has the 'success' class.
            $(formMessages).removeClass('error').addClass('success');

            // Set the message text.
            $(formMessages).html(response); // < html();

            // Clear the form.
            $('').val('')
        }).fail(function (data) {
            // Make sure that the formMessages div has the 'error' class.
            $(formMessages).removeClass('success').addClass('error');

            // Set the message text.
            var messageHtml = data.responseText !== '' ? data.responseText : 'Oops! An error occured and your message could not be sent.';
            $(formMessages).html(messageHtml); // < html()
        });

    });

    function validateReg() {
        var valid = true;

        if (!$("#sname").val()) {
            $("#sname-info").html("(required)");
            $("#sname").css('background-color', '#FFFFDF');
            valid = false;
        }

         if (!$("#ssurname").val()) {
            $("#ssurname-info").html("(required)");
            $("#ssurname").css('background-color', '#FFFFDF');
            valid = false;
        }

         if (!$("#spword").val()) {
            $("#spword-info").html("(required)");
            $("#spword").css('background-color', '#FFFFDF');
            valid = false;
        }  


        if(!$("#sEmail").val()) {
        $("#sEmail-info").html("(required)");
        $("#sEmail").css('background-color','#FFFFDF');
        valid = false;
    }
    if(!$("#sEmail").val().match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/)) {
        $("#sEmail-info").html("(invalid Email)");
        $("#sEmail").css('background-color','#FFFFDF');
        valid = false;
    }

        if(!$("#sPhone").val()) {
        $("#sPhone-info").html("(required)");
        $("#sPhone").css('background-color','#FFFFDF');
        valid = false;
    }

        return valid;
    }
})
  • 写回答

1条回答 默认 最新

  • weixin_33737134 2015-09-01 11:52
    关注

    Your validation function always returns "false", that is why request isn`t sent. This happens because email validation never passes:

    if(!$("#sEmail").val()) {
    }
    

    In its turn, that happens due to fact that you have a span and an input with same id. Try to change it:

    <span id="sEmail" style="color:red"></span> 
    

    See my fiddle: http://jsfiddle.net/9cs65xfn/

    评论

报告相同问题?

悬赏问题

  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?