简体   繁体   中英

Returning TypeError: document.getElementById(…) is null

I have an Ajax code that am using to get data from my php page, but it is not returning any data back to the Ajax, returning null value.

function getType(str){
$("#selectCat").html("Select the Catagory of " +str);

    if (str.length == 0){ 
                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)
    {
        document.getElementById('category').innerHTML = xmlhttp.responseText;
    }
}
 xmlhttp.open("GET","OrderItem.php?q="+str,true);
 xmlhttp.send();
 }

and here is my php code

session_start();
    $currentUser = $_SESSION["userID"];
    include("include/dbcommon.php");
$value = $_GET['q'];
//echo $value;
    $sqlString = "";
   if($value == "dish"){
        $sqlString = "select name from dishcategory";
       }else if($value == "drinks"){
        $sqlString ="select name from drinkscategory";
   }else if($value == "desserts"){
        $sqlString = "select name from dessertcategory";
}
    $rs = customQuery($sqlString);
while($data= db_fetch_array($rs)){

    echo "<option value='".$data['name']."'>".$data['name']."1</option>";

}

and my HTML part

<TABLE style="WIDTH:85%" cellSpacing=1 cellPadding=1 width="75%">
  <TBODY>
    <TR>
       <TD>&nbsp;<STRONG>Ordered Item Category</STRONG></TD></TR>
     <TR>
       <TD>&nbsp;<input type="Radio" name="itemType" id="" class="selebtn" value="dish" onClick="getType(this.value)"> Dish <input type="Radio" name="itemType" id="" class="selebtn" value="drinks" onClick="getType(this.value)"> Drinks <input type="Radio" name="itemType" id="" class="selebtn" value="desserts" onClick="getType(this.value)"> Desserts
       </TD></TR></TBODY></TABLE>
     <TABLE>
  <TBODY>
    <TR><TD><span id="selectCat"></span></TD></TR>
    <TR><TD><span id="selePan"><select name="itemcat" id="itemcat"><option value="">select</option><span id="category"></span></select></span></TD></TR>
</TBODY>
     </TABLE>

please i can't get the why it is returning null value? thanks in advance

Your HTML is invalid. You cannot have a span element as a child element of a select. Your problem is probably due to the browser performing error recovery and discarding the span element (that is certainly how Chrome reacts to your code).

Use a validator .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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