Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2011
    Location
    NY
    Posts
    33
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Ajax Issue in 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 Issue in php-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
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    try using this[this.selectedIndex].value instead of this.value

  • #3
    New Coder
    Join Date
    Sep 2011
    Location
    NY
    Posts
    33
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by NancyJ View Post
    try using this[this.selectedIndex].value instead of this.value
    Thanks for the reply. I had tried this way but it was not working with me , may be i was doing something wrong as i haven't used this code before. But now i have resolved this issue by using if condition before displaying response.
    here is the code:
    PHP Code:
    var res=obj.responseText;
                if(
    md=='city')
                {
                    
                
    document.getElementById("market").innerHTML=res;
                 
                    }
                    else {
                        
    document.getElementById("city").innerHTML=res;
                        
                        } 
    But now i cant get 100% good result like i am not able to get the result on third menu as i use to change value in the first menu. Hop you got my point.
    Thanks.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This does not appear to be a PHP issue.
    Although there are errors here PHP wise, they are minor and won't prevent the code from running with the exception of the short tags which may prevent the code from running.

    Navigate to dcode.php?mode=state&id=x where x is a valid id to retrieve. If that provides results, the problem is either your resulting XML code or the AJAX.

  • #5
    New Coder
    Join Date
    Sep 2011
    Location
    NY
    Posts
    33
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I have resolved the issue using if condition on Ajax Code. But it Still not working fully for me as i want to use. Like as i have already told that there are 3 drop down menu. Now i am getting result on all three menu list but as i change the value in first drop down menu then the values on all other 2 child menu should change automatically. For this i am calling a function on onchange event of first drop down but it works till second and i cant get the value for third one.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by fredrikrob View Post
    I have resolved the issue using if condition on Ajax Code. But it Still not working fully for me as i want to use. Like as i have already told that there are 3 drop down menu. Now i am getting result on all three menu list but as i change the value in first drop down menu then the values on all other 2 child menu should change automatically. For this i am calling a function on onchange event of first drop down but it works till second and i cant get the value for third one.
    Is the result of calling the php script directly with the given arguments correct? If it is, this is not a PHP issue, this is a javascript one.
    All the signs you have here so far indicate a javascript problem.


  •  

    Posting Permissions

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