Getting issue in PHP and Ajax list/dropdown menu.

Discussion in 'Programming' started by fredrikrob, Apr 17, 2012.

  1. #1
    Hi,


    I have some problem in ajax like i am not able to retrieve data through Ajax. Please look at the attachment pic to better understand the scenario.
    php.JPG
    I am trying to get all the values from a database which have 4 states and 3 cities in each state and third 2 or three market in each citi.
    Here i am getting issue for the first time when the page is first time loaded. I have called a javascript function on first page which gets the value for state and cities is loaded but the market menu remains the blank. I want to get the value in market according the whatever city is selected on the city tab. Hope you can understand what i am looking for. I have tried to find out the problem but all in vain. Here i am attaching all the php code below and hop i need not to upload database details but if you require then i will add too. Thank in advance.

    Here is the main designing file code:
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script type="text/javascript">function Action(link,md){    var obj,url;
    url="dcode.php?mode="+md+"&id="+link+"&r="+Math.random();
     try {        obj=new XMLHttpRequest();        }        catch(e)            {                try {                     obj=new ActivexObject("Microsoft.XMLHTTP");                                        }                                        catch(e) {                                                alert(e);                                                }                                }          obj.open("GET",url,true);  obj.send(null); obj.onreadystatechange=function() {     if(obj.readyState==4)        {                                var res=obj.responseText;            if(md=='city')            {                            document.getElementById("market").innerHTML=res;                             }                else {                    document.getElementById("city").innerHTML=res;                                        }                        //document.getElementById("city").innerHTML=res;                    }                            }      }</script>
    
    <?
    
    ?>
    
    
    
    </head>
    <body>
    <form action="" method="get"><table width="200" border="1" align="center">  <tr>    <th scope="row"><label>      <select name="state" id="state" style="width:150px;" onchange="Action(this.value,'state')">           <?      mysql_connect("localhost","root","") or die("check server");        mysql_select_db("students")or die("check database");
        $str="select sno,sname from state";    $rs=mysql_query($str);        while(list($scode,$stname)=mysql_fetch_array($rs))    {              echo "<option value='$scode'>$stname</option>";            }      ?>      </select>    </label></th>  </tr>  <tr>    <th scope="row"><label>      <select name="city" id="city" style="width:150px;" onchange="Action(this.value,'city')">                 </select>    </label></th>     </tr>   <tr>    <th scope="row"><select name="market" id="market" style="width:150px;">                    </select></th>  </tr> </table></form>
    <script type="text/javascript">Action(101,'state');
    
    </script></body></html>
    PHP:


    Here is the Code file code:
    
    
    <?
    
     mysql_connect("localhost","root","") or die("check server");mysql_select_db("students")or die("check database");
    
    
    if($_REQUEST["mode"]==state)
    {    $id=$_REQUEST["id"];        $str="SELECT citycode,cityname    FROM city    WHERE statecode    IN (        SELECT statecode    FROM city    JOIN state ON city.statecode = $id)";        $rs=mysql_query($str);        while(list($citycode,$cityname)=mysql_fetch_array($rs))       {        echo "<option value='$citycode'>$cityname</option>";            }            }            if($_REQUEST["mode"]==city)        {        $cid=$_REQUEST["id"];        $str="SELECT mcode,mname    FROM market    WHERE citicode    IN (        SELECT citicode    FROM market    JOIN city ON market.citicode = $cid)";    $rs=mysql_query($str);            while(list($mcode,$mname)=mysql_fetch_array($rs))       {        echo "<option value='$mcode'>$mname</option>";            }                                        }    ?>
    
    
    
    PHP:
     
    fredrikrob, Apr 17, 2012 IP
  2. e-abi

    e-abi Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    38
    #2
    Could you make your code more readable?

    And also do you have firebug extension installed? It has the ability to analyze all the ajax requests and responses.
     
    e-abi, Apr 17, 2012 IP
  3. fredrikrob

    fredrikrob Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well the code was readable but digital-point editor made it look like this. May be copying and pasting these code on notepad ++ or other editor may help you. I haven't tried the firebug extension for this issue will try it.
     
    fredrikrob, Apr 17, 2012 IP