繁体   English   中英

使用AJAX请求处理空白选项值

[英]Handling blank option value with AJAX requests

我在查看代码的DOM树时注意到, <option value="">更改为<option value> ,这在我尝试通过AJAX检查用户是否在下拉菜单中选择了空白选项时引起了问题。下菜单。

我使用AJAX将下拉列表中选择的值传递给下面的PHP代码,但是如果该选项为空,则会出现错误。 这是我尝试过的:

形成:

<label for="district" class="general medium">District</label>
<select id="district" name="district" class="textbox short_field" onchange="show_locations(this.value);">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>

AJAX脚本:

<script>
function show_locations(str) {

if (str=="") {
    document.getElementById("locations_dropdown").innerHTML="";
    return;
    }

if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        var a = xmlhttp.responseText;
            a = JSON.parse(a);
            document.getElementById("locations_dropdown").innerHTML=a.location;
            document.getElementById("dm").innerHTML=a.dm;
            document.getElementById("dm_email").innerHTML=a.dm_email;
    }
}

xmlhttp.open("GET","includes/get_locations.php?d="+str,true);
xmlhttp.send();

}
</script>

然后,我的PHP文件get_locations.php检索d的值,如下所示:

<?php
$district_id = $_REQUEST['d'];

if($district_id == NULL){
    $location_box = '<select id="location_id" name="location_id" class="textbox short_field required" onchange="show_sm(this.value);">
    <option value=""></option>
    </select>';

    $dm_box = '<input type="text" id="district_manager" name="district_manager" class="textbox short_field" value="">';

    $dm_email = '<input type="email" id="send_to_dm" name="send_to_dm" class="textbox long_field" value="">';

    } else {
        $location_box = '<select id="location_id" name="location_id" class="textbox short_field required" onchange="show_sm(this.value);">
        <option value="#"></option>';

        $query = 'SELECT l.id, l.banner_id, l.location, b.code
                  FROM locations l
                  INNER JOIN banners b ON (l.banner_id = b.id)
                  WHERE district_id =' . $district_id;

        $result = mysql_query($query, $connection);
        if (!result) {
            die("Database query failed: " . mysql_error());
        }

        while ($row = mysql_fetch_array($result)) {
          $location_box .= '<option value="' . $row['id'] . '"';
          if($row['id'] == $location) { $location_box .= ' selected';} ;
          $location_box .= '>' . $row['code'] . ' ' . $row['location'] . '</option>';
        }

        $location_box .= '</select>';
//Get DM Info ...

        $query = "SELECT id FROM locations WHERE district_id = '{$district_id}' AND number = 0";

        $result = mysql_query($query, $connection);
            if (!result) {
                die("Database query failed: " . mysql_error());
            }

            while ($row = mysql_fetch_array($result)) {
                $dm_location = $row['id'];
            }

        $query = "SELECT first_name, last_name, email FROM users WHERE location_id = " . $dm_location . " AND position_id = 2";

        $result = mysql_query($query, $connection);
            if (!result) {
                die("Database query failed: " . mysql_error());
            }

            if (mysql_num_rows($result)==0) {
                    $dm_box = '<input type="text" id="district_manager" name="district_manager" class="textbox short_field" value="Not found. Enter manually.">';
                    $dm_email = '<input type="email" id="send_to_dm" name="send_to_dm" class="textbox long_field" value="Not found. Enter manually.">';
            }

            while ($row = mysql_fetch_array($result)) {
            $dm = $row['first_name'] . ' ' . $row['last_name'];
            $email = $row['email'];

            $dm_box = '<input type="text" id="district_manager" name="district_manager" class="textbox short_field" value="' . $dm . '">';

            $dm_email = '<input type="email" id="send_to_dm" name="send_to_dm" class="textbox long_field" value="' . $email . '">';
            }

}



echo json_encode(array('location'=>$location_box,'dm'=>$dm_box,'dm_email'=>$dm_email));
?>

但这不能识别出所选值具有value=""所以我猜$ _REQUEST ['d']不是NULL吗?

我也尝试过if(!isset($_REQUEST['d'])); 这也不起作用。

该代码只是给我错误。

任何帮助,将不胜感激。

您可以在js端使用它;

function show_locations() {

    var e = document.getElementById("district");
    var str = e.options[e.selectedIndex].value;

    if (str.length < 1) {
        document.getElementById("locations_dropdown").innerHTML="";
        return;
        }

    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
        } else { // code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            var a = xmlhttp.responseText;
                a = JSON.parse(a);
                document.getElementById("locations_dropdown").innerHTML=a.location;
                document.getElementById("dm").innerHTML=a.dm;
                document.getElementById("dm_email").innerHTML=a.dm_email;
        }
    }

    xmlhttp.open("GET","includes/get_locations.php?d="+str,true);
    xmlhttp.send();

}

然后在选择框中;

<select id="district" name="district" class="textbox short_field" onchange="show_locations();">

并在php端检查;

if(empty($district_id)) {...

您可以在此处查看工作演示: jsfiddle

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM