验证后Javascript函数无效

时间:2017-12-04 16:13:39

标签: javascript visual-studio-2017

目前正在执行一项功能,用户可以输入他们的查询,这些查询将被发送到数据库并由管理员检索,但添加验证代码后添加查询的功能停止工作,问题在于验证但我不能似乎找不到错误。

我的js代码:

var userid = localStorage.getItem("userid");
var Texttitle;
var Textenquiry;

function validate() {
    var validate = true;

    if (Texttitle == "") {
        alert("Title cannot be empty");
        $("#Texttitle").focus();
        validate = false;
    }

    if (Textenquiry == "") {
        alert("Enquiry cannot be empty");
        $("#Textenquiry").focus();
        validate = false;
    }


    return validate;
}


function addenquiry() {

    Texttitle = $("#Texttitle").val();
    Textenquiry = $("#Textenquiry").val();

    if (validate()) {

        var xmlhttp = new XMLHttpRequest();
        var url = serverURL() + "/addenquiry.php";
        url += "?userid=" + userid + "&Texttitle=" + Texttitle + "&Textenquiry=" + Textenquiry;
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            addEnquiry(xmlhttp.responseText);
            }
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send();
    }

function addEnquiry(response) {
    var arr = JSON.parse(response);
    if (arr[0].result == 1) {
        alert("Thank You for your enquiry!")
    }
    else {
        alert("Sorry please try again");
    }

}

我的Html:

 <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
     <label class="mdl-textfield__label" for="Texttitle">Title</label>
       <input class="mdl-textfield__input" rows="2" type="text" id="Texttitle">
 </div>
    <br>
    <br>
 <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
       <label class="mdl-textfield__label" for="Textenquiry">Message</label>
         <input class="mdl-textfield__input" rows="2" type="text" id="Textenquiry">
 </div>
    <br>
    <br>
    <button class="mdl-button mdl-color--primary mdl-js-button mdl-color-text--white full-width" onclick="addenquiry()">Submit</button>
 <div class="material-icons mdl-badge mdl-badge--overlap">�</div>

在我输入验证码之前,我的功能运行良好,在我尝试验证后,该功能无法将查询发送到我的数据库,会感谢一些帮助:)。

2 个答案:

答案 0 :(得分:0)

在验证功能中,为什么要复制逻辑?你有:

if (Texttitle == "") {
    alert("Title cannot be empty");
    $("#Texttitle").focus();
    validate = false;

} if (Textenquiry == "") {
    alert("Enquiry cannot be empty");
    $("#Textenquiry").focus();
    validate = false;
}

两次。

此外,您的问题可能是验证功能结束时的*/

答案 1 :(得分:0)

你只是有一些语法错误。首先,你要在自己内部重新定义你的验证功能(可能应该只是调用一些不同的变量,例如&#39;验证&#39;。但是,你的主要问题只是你错过了你的addenquiry方法的结束。我&#39 ; d还建议使您的方法名称更加独特,而不仅仅是改变名称的大小,因为调试时代码变得越来越噩梦。

试试这个:

        var userid = localStorage.getItem("userid");
    var Texttitle;
    var Textenquiry;

    function validate() {
        var validated = true;

        if (Texttitle == "") {
            alert("Title cannot be empty");
            $("#Texttitle").focus();
            validated = false;
        }

        if (Textenquiry == "") {
            alert("Enquiry cannot be empty");
            $("#Textenquiry").focus();
            validated = false;
        }


        return validated;
    }


    function addenquiry() {

        Texttitle = $("#Texttitle").val();
        Textenquiry = $("#Textenquiry").val();

        if (validate()) {

            var xmlhttp = new XMLHttpRequest();
            var url = serverURL() + "/addenquiry.php";
            url += "?userid=" + userid + "&Texttitle=" + Texttitle + "&Textenquiry=" + Textenquiry;
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                addEnquiry(xmlhttp.responseText);
                }
            }
            xmlhttp.open("GET", url, true);
            xmlhttp.send();
        }
    }

    function addEnquiry(response) {
        var arr = JSON.parse(response);
        if (arr[0].result == 1) {
            alert("Thank You for your enquiry!")
        }
        else {
            alert("Sorry please try again");
        }

    }