weixin_33724046 2018-03-27 13:43 采纳率: 0%
浏览 20

AJAX返回和onsubmit HTML [重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call" dir="ltr">How do I return the response from an asynchronous call?</a>
                            <span class="question-originals-answer-count">
                                (38 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2018-03-27 15:11:27Z" class="relativetime">2 years ago</span>.</div>
        </div>
    </aside>

I'm trying to make a form to signup to my site but AJAX is not returning false for an onsubmit. Here is my code:

var c = false;
var h = true;
function hi(g, j, k) {
    if (g == j) {
        h = false;
    }
}
function validateForm() {
    var x = document.forms["hi"]["pass1"].value, v = document.forms["hi"]["pass2"].value, b = document.forms["hi"]["user"].value;
    if (x != v) {
        alert("Passwords must match");
        return c;
    }
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            var a = this.responseText.split("
")
            for (var n in a) {
                console.log(a);
                console.log(a[n]);
                console.log(b);
                hi(a[n], b, this.responseText)
                if (a[n] == b) {
                    alert("Username already exists")
                    return c;
                };
            };
        };
    };
    xmlhttp.open("GET", "http://www.ncpcs.com/carter/user.txt", true);
    xmlhttp.send();
    return h;
}`

and the onsubmit has return validateForm()

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33698823 2018-03-27 15:09
    关注

    Here's a different approach

    First, instead of using onsubmit attribute on your form, use an onclick attribute on a normal (i.e. type="button") button. Upon clicking on the button, it will call the validateForm() function.

    <form name="hi" action="otherPage.html">
        <input type="text" name="user" value="" />
        <input type="password" name="pass1" value="" />
        <input type="password" name="pass2" value="" />
        <button type="button" onclick="validateForm()">Submit</button>
    </form>
    

    Within function, if there is an error, it will simply alert. If all goes well, it will submit the form.

    function validateForm() {
        // use better variable names
        var form = document.forms["hi"],
            pass1 = form["pass1"].value, 
            pass2 = form["pass2"].value, 
            user = form["user"].value;
    
        if (pass1 != pass2) {
            alert("Passwords must match");
            // this is only used to prevent execution of the rest of the function
            return;
        }
    
        xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                var a = this.responseText.split("
    ")
                for (var n in a) {
                    console.log(a);
                    console.log(a[n]);
                    console.log(user);
                    //hi(a[n], user, this.responseText)
                    if (a[n] == user) {
                        alert("Username already exists")
                        return;
                    }
                };
                // at this point, everything went well so submit the form 
                form.submit();
            };
        };
        xmlhttp.open("GET", "http://www.ncpcs.com/carter/user.txt", true);
        xmlhttp.send();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答