Note that there are some explanatory texts on larger screens.

plurals
  1. POcalling DB from javascript throwing error
    primarykey
    data
    text
    <p>i have a simple php page postArticle.php with two dropdown list with categories and subcategories and subcategories generates on the basis of categories.</p> <p>for generating subcategories i am using java script from which i am calling another php file(data.php)</p> <p>the code postarticle.php is</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; function getSubCategories(l1,l2) { var response; var xmlHttp; if(l1.selectedIndex&gt;=1) { try{ var id=document.getElementById('listCategory').value; //alert("hello "+id); l2.disabled=false; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari var xmlHttp=new XMLHttpRequest(); } else {// code for IE6, IE5 var xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } //alert("hello 1"); xmlHttp.open("POST","data.php?q="+id,true); //alert("id passed is : " +id); xmlHttp.onreadystatechange = function(){ //alert("inside fn1"); if (xmlHttp.readyState == 4) { //alert("inside if fun"); //alert(xmlHttp.responseXML.getElementsByTagName("div")[0]); //alert("response text : "+xmlHttp.responseText); response=xmlHttp.responseText; } //alert("inside fn2"); } //alert("hello 3"); xmlHttp.send("data.jsp?q="+id); //alert('hello 4'); var resArray=new Array(); //alert('hello 5'); response=response.replace(/^\s+|\s+$/g,""); resArray=response.split("#"); //alert('hello 6'); //alert("response array : "+resArray); for(x in resArray) { //alert(resArray[x].substring(0, resArray[x].indexOf("*"))); //alert(resArray[x].substring(resArray[x].indexOf("*")+1)); if(resArray[x].substring(0, resArray[x].indexOf("*"))!=" " &amp;&amp; resArray[x].substring(resArray[x].indexOf("*")+1)!="") { var OptNew = document.createElement('option'); OptNew.text = resArray[x].substring(resArray[x].indexOf("*")+1); OptNew.value = resArray[x].substring(0, resArray[x].indexOf("*")); try { //for IE earlier than version 8 l2.add(OptNew,l2.options[null]); } catch (e) { l2.add(option,null); } } } }catch(e){alert(e.toString());} } else { return; } var newOpt = new Option(selText[i], selValues[i]); theSel.options[i] = newOpt; } &lt;/script&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt; &lt;title&gt;Untitled Document&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;table border="1" width="100%"&gt; &lt;tr id="header" &gt; &lt;td height="30%" width="100%" colspan="3"&gt; &lt;?php include("Header.php"); ?&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr id="body"&gt; &lt;td height="65%" width="15%" valign="center"&gt; &lt;table width="100%"&gt; &lt;tr&gt;&lt;td&gt; &lt;?php include("LeftPanel.php"); ?&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/td&gt; &lt;td height="65%" width="75%" valign="center"&gt; &lt;form method="post" action="PostArticle_Back.php"&gt; Title: &lt;input type="text" name="txtTitle" id="txtTitle"&gt; &lt;br&gt; Content : &lt;textarea id="txtContent" name="txtContent" rows="5" cols="50"&gt;&lt;/textarea&gt; &lt;br&gt; &lt;p&gt;select Category :&lt;/p&gt; &lt;select id="listCategory" name="listCategory" onchange="getSubCategories(listCategory,listSubCategory)"&gt; &lt;option value="0"&gt;Select Category&lt;/option&gt; &lt;?php $con=mysql_connect("localhost","root",""); if(!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("articles_db",$con); $result = mysql_query("select CATEGORY_ID, CATEGORY_NAME from TBL_CATEGORIES"); while($row = mysql_fetch_array($result)) { ?&gt; &lt;option value="&lt;?php echo $row['CATEGORY_ID'] ; ?&gt;" &gt; &lt;?php echo $row['CATEGORY_NAME'] ; ?&gt; &lt;/option&gt; &lt;?php } ?&gt; &lt;/select&gt; &lt;br&gt; &lt;p&gt;select SubCategory :&lt;/p&gt; &lt;select id="listSubCategory" name="listSubCategory" disabled="true"&gt; &lt;option value="0"&gt;select Subcategory&lt;/option&gt; &lt;/select&gt;&lt;br&gt; &lt;input type="submit" value="Post Article"/&gt; &lt;/form&gt; &lt;/td&gt; &lt;td height="65%" width="10%" valign="center"&gt; Right panel &lt;/td&gt; &lt;/tr&gt; &lt;tr id="footer"&gt; &lt;td height="5%" colspan="3"&gt; Footer &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>but it throws an error <strong>TypeError : undefined is null or not an object</strong></p> <p>the problem what i find till now is that the data.php is taking time to send response </p> <p>if(xmlHttp.readyState == 4) is taking time to execute.</p> <p>because when i add alert statements in between code it works fine.</p> <p>what i tried till now:</p> <ol> <li><p>i tried to add while loop outside if condition but broweser gives a warning : <strong>script is making your webpage slow</strong></p></li> <li><p>i tried to put the code(the code for adding repose to drop down list) in if(xmlHttp.readyState == 4) condition but it did not gave any response.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload