用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