Jump to content

Recommended Posts

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.  .

 

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:

<!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:

 

<? 


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>"; 
     
        } 
         
    } 
     

?>

post-127764-13482403427071_thumb.jpg

Link to comment
https://forums.phpfreaks.com/topic/260918-need-help-in-ajax-with-php/
Share on other sites

Your query does not look healthy

$str="SELECT citycode,cityname 
FROM city 
WHERE statecode 
IN ( 
SELECT statecode 
FROM city 
JOIN state ON city.statecode = $id)"; 

 

This is what I think you want

$str="SELECT citycode, cityname 
FROM city 
WHERE statecode = $id";

 

And you should has something in your first dropdown. Yours is empty. And I would not use 'action' as the name of a function. I'm sure it's a reserved word, but too lasy to look it up. Try "get_select" instead.

Your query does not look healthy

$str="SELECT citycode,cityname 
FROM city 
WHERE statecode 
IN ( 
SELECT statecode 
FROM city 
JOIN state ON city.statecode = $id)"; 

 

This is what I think you want

$str="SELECT citycode, cityname 
FROM city 
WHERE statecode = $id";

 

And you should has something in your first dropdown. Yours is empty. And I would not use 'action' as the name of a function. I'm sure it's a reserved word, but too lasy to look it up. Try "get_select" instead.

 

Thanks for the reply.  Well i had a message code in my file before but i had issue in Ajax and javascript as i was unable to print the double result. With the help of ajax i was getting one 2 response one for city and one for market dropdown  menu. But i had only one id selected to print the result. For that i have used if condition and now it's working.  Here is the 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.

OK I guess I don't know what you want. I see three drop down menus that you want populated, right?

I don't know what you want in the top dropdown because it is blank. But think it's the state. Right?

The second dropdown has 'select city' in it and I thought you needed to find the cities that came from the first dropdown choice. Which should be the state or as you call it statecode and that came from the first or top dropdown.

 

Is that right?

 

And then you want to get the market and put that in the select market dropdown, the third one. Is that right? And where (table and column) is that info and what is the city column needed for that extraction?

 

I gave you the query to get the second dropdown information after the state was picked in the first.

You say this don't work? Your not getting the city names?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.