Submit Your Article Forum Rules

Results 1 to 3 of 3

Thread: Having issue with Ajax and php

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2

    Having issue with Ajax and php

    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:
    PHP Code:

    <html>
    <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>
    Here is the Code file code:
    PHP 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>";
        
            }
            
            
            
            
            }
        
    ?>

  2. #2
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,823
    Not having MYSQL experience, this may not be of any help. When I see unquoted strings usually a flag goes up.
    PHP Code:
    if($_REQUEST["mode"]==state
    Is this correct? or possibly the point of your difficulty?

  3. #3
    WebProWorld MVP DaveSawers's Avatar
    Join Date
    Dec 2006
    Location
    Lunenburg, Nova Scotia, Canada
    Posts
    762
    To find errors in the AJAX bit, echo the responseText into a div or similar. If the code doesn't compile for some reason, the result text will contain the error message.
    Dynamic Software Development
    www.activeminds.ca

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •