ajax if语句不能正常工作

时间:2013-09-19 16:51:57

标签: javascript php ajax if-statement

嗨我有一些ajax编码,其中if条件根本不起作用,每当程序执行else语句时,只有程序才能满足if语句。

<script type="text/javascript">
    function CheckDetails()
    {
        var http = false;

        if (navigator.appName == "Microsoft Internet Explorer") {
            http = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            http = new XMLHttpRequest();
        }

        var rid = document.new_booking.ph_number.value;

        http.onreadystatechange = function() {
            if (http.readyState == 4) {
                var str_d = http.responseText;
                if (str_d == "no") {
                    document.getElementById('cus_name').focus();
                } else {
                    var str_details = http.responseText;
                    var arr_details = str_details.split("~");
                    document.new_booking.cus_name.value = arr_details[0];
                    document.new_booking.pick_aline1.value = arr_details[1];
                    document.new_booking.pick_aline2.value = arr_details[2];
                    document.new_booking.pick_area.value = arr_details[3];
                    document.new_booking.pick_pincode.value = arr_details[4];
                    document.new_booking.drop_aline1.focus();
                }
            }
        }
        http.open("GET", "ajax.php?evnt=det&rid=" + rid);
        http.send();
    }
</script>

及其ajax.php文件在下面给出

<?php
    if ($_GET['evnt'] == 'det') {
        $rid = $_GET['rid'];
        include("configure.php");
        $select = mysql_query("select * from new_booking where ph_number = '$rid'");
        $count = mysql_num_rows($select);
        if ($count > 0) {
            $row = mysql_fetch_array($select);
            echo $row['cus_name']
                . "~" . $row['pick_aline1']
                . "~" . $row['pick_aline2']
                . "~" . $row['pick_area']
                . "~" . $row['pick_pincode']
                . "~" . $row['drop_aline1']
                . "~" . $row['drop_aline2']
                . "~" . $row['drop_area']
                . "~" . $row['drop_pincode'];
        } else {
            echo "no";
        }
    }
?>

3 个答案:

答案 0 :(得分:1)

您可以使用Chrome(或Chromium)打开您的页面,然后使用内置调试程序(Ctrl + Shift + I,“控制台”标签)调试您的javascript代码。我想你会在那里看到一些JS错误。

基本上,您的代码工作正常(至少当我从中移除了所有数据库访问权限时,因为我没有您的数据库)。

如果您不喜欢Chrome,请使用Firefox和FireBug扩展程序。在“网络”页面上,您可以看到您的ajax请求已执行(或未执行)。

答案 1 :(得分:0)

如果我不得不猜测,我会说某些空白正在偷偷进入你的AJAX响应。由于"no "不等于"no",因此始终会点击您的else分支。

您可以考虑发回与空白无关的值。您可以重写整个混乱以使用JSON,这将需要在两端更少的工作:

// PHP:
if ($_GET['evnt'] == 'det') {
    $rid = $_GET['rid'];
    include("configure.php");
    $select = mysql_query("select * from new_booking where ph_number = '$rid'");
    $count = mysql_num_rows($select);
    if ($count > 0) {
        $row = mysql_fetch_array($select);
        // YOU PROBABLY WANT TO WHITELIST WHAT YOU PASS TO JSON_ENCODE
        echo json_encode($row);
    } else {
        echo json_encode([ "error" => "no" ]);
    }
}

// js:
http.onreadystatechange = function() {
    if (http.readyState == 4) {
        var str_d;
        try {
            str_d = JSON.parse(http.responseText);
        } catch(e) {
            str_d = false;
        }

        if (str_d && str_d.error === "no") {
            document.getElementById('cus_name').focus();
        } else {

            document.new_booking.cus_name.value = str_d.pick_aline1;
            document.new_booking.pick_aline1.value = str_d.pick_aline2;
            document.new_booking.pick_aline2.value = str_d.pick_area;
            // etc...
        }
    }
}

答案 2 :(得分:0)

我遇到了类似的问题。这对我有用。

if (str_d.replace(/^\s+|\s+$/g, '') == "no")