SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2010
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    loading cities using AJAX and PHP....Please help.

    Hi,

    I am trying to load city combo using AJAX and PHP.....but its showing nothing...

    Please help..

    This is AJAX

    HTML Code:
    // JavaScript Document
    // JavaScript Document
    
    var xmlHttp
    // Start of loading Constituency //
    function loadcity(state_search)
    {
    	var value=document.getElementById('inpstate').value;
    	//var lang=document.getElementById("language").value;	
    	xmlHttp=GetXmlHttpObject();
    	if (xmlHttp==null)
    	 {
    	 alert ("Browser does not support HTTP Request");
    	 return;
    	 }
    	var url="controllers/contact_controller.php";
    	url=url+"?work=cityinfo";
    	url=url+"&state_id="+value;
    	//url=url+"&lang_id="+lang;
    	xmlHttp.onreadystatechange=addcityname;
    	xmlHttp.open("GET",url,true);
    	xmlHttp.send(null);
    }
    
    function addcityname() 
    { 
    	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    	 { 
    		val=xmlHttp.responseText;
    		//alert(val);
    		document.getElementById('city_infodivxxx').innerHTML=xmlHttp.responseText;
    		//document.getElementById('txtothercity').disabled=false;
    		//loadParties('subcat_id');
    	 } 
    	 
    }
    // End of loading Constituency CITY //
    
    function GetXmlHttpObject()
    {
    	var xmlHttp=null;
    	try
    	 {
    	 // Firefox, Opera 8.0+, Safari
    	 xmlHttp=new XMLHttpRequest();
    	 }
    	catch (e)
    	 {
    	 //Internet Explorer
    	 try
    	  {
    	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	  }
    	 catch (e)
    	  {
    	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	  }
    	 }
    	return xmlHttp;
    }
    Here's PHP how I am doing this....

    PHP Code:
    if($_POST['work']=="cityinfo")
    {
        
    $stateid=$_GET['state_id'];
        
    $contact = new contact();
        
    $data->$contact->getcity($_POST);
         
    $city "<select gtbfieldid=\"6\" name=\"txtothercity\" id=\"txtothercity\" tabindex=\"4\">";
        while(
    $row mysql_fetch_array($data)) {
            
    //extract($data);
            
    $city"<option value=\"".$ct_id."\">".$ct_name."</option>";
        }
            
    $city "</select>"
        
    return $city;
    }

    //Class File

    class contact extends function
    {
    function 
    getcity($state)
        {
            
    $sql="select * from tblcity where st_id =$state";
            
    $result mysql_query($sql);
            return 
    $result;
        }

    I think I am having problem in PHP section....in returning data...

    Your help would be appreciated.

    Thanks

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code:
    $data->$contact->getcity($_POST);
    You're passing the entire $_POST array to the function, and the function expects only the state.

    By the way, all $_POST data is user input, and should be sanitized before using it in a query.


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
  •