SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Location
    NY
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help in Ajax with 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.ajax.JPG

    Here are three fields first one will go with State, second will go with the city within the state and third one is for the market's or showroom within the city. I can easily retrive the data till first 2 option. Like if i select the State A and then i can see all the cities under the state A but i am not able to get the output in the market section. As Ajax query have only one response variable. I have spent lot's of hour in this and now have give up. Please help me in this. I am attaching the both php and files to get it clear.

    here is the main desiging file:
    PHP 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; 





     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; 
                 
                alert(res); 
                document.getElementById("city").innerHTML=res; 
                 
            } 
           
           
           
           
        } 
       
         

    </script> 


    <? 


    ?>
     




    </head> 

    <body> 

    <form action="dcode.php" 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')"> 
          <option value="city">Select city</option> 
          <? 
          $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>"
         
        } 
           
          
    ?> 
          </select> 
        </label></th> 
        
      </tr> 
      <tr> 
        <th scope="row"><select name="market" id="market" style="width:150px;"> 
          <option value="market">Select Market</option> 
          <? 
          $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>"
         
        } 
           
          
    ?> 
        </select></th> 
      </tr> 
    </table> 
    </form> 

    <script type="text/javascript"> 
    Action(101,'state'); 
    </script> 
    </body> 
    </html>
    and here is the main Code file where i have written php query to get the result:
    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>"
         
            } 
             
        } 
         

    ?>

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    266
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    When is selected the option in 3rd select list, you need to use in the Select SQL query the values from the other two select lists. These values can be stored in session.
    Here is a ready made script: Multiple Select Dropdown List with AJAX that can be used for what you want to make.
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Location
    NY
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MarPlo View Post
    When is selected the option in 3rd select list, you need to use in the Select SQL query the values from the other two select lists. These values can be stored in session.
    Here is a ready made script: Multiple Select Dropdown List with AJAX that can be used for what you want to make.
    Thanks for the link you have given no doubt it will solve my issue. I will check it and will try my code. Again thanks for such a great solution.

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Location
    NY
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have got the solution as i was not getting any result before. But now i have resolved the issue but as 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 city.

    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. Thanks in advance.

    Here is the Updated main designing file code:
    PHP 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>
    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>";
        
            }
            
            
            
            
            }
        
    ?>


Bookmarks

Posting Permissions

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