Part of the EllisLab Network
   
 
how to use AJAX in CI
Posted: 22 May 2008 02:00 AM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  303
Joined  06-25-2007

I like to use AJAX to get State using Zipcode.
Here is my controller

class Test extends Controller
{
  
function Test()
  
{
    parent
::Controller();
  
}
  
function StateByZip($ZipCode="40011")
  
{
    $ch 
curl_init();
    
curl_setopt($chCURLOPT_RETURNTRANSFER1);  
    
curl_setopt($chCURLOPT_URL ,"http://www.shopno-dinga.com/WebService/zipinfo/info_by_zip.php?zip=".$ZipCode);
    
$ZipDetail=curl_exec($ch);    // Output: State=KY&City=CAMPBELLSBURG&County=HENRY
    
curl_close($ch);
    echo 
$ZipDetail;
   
}
   
function ZipCity()
   
{
      $this
->load->view("view_test_zip");
   
}

Here is my view file view_test_zip.php. In addition, this file also have AJAX XHLHTTP Request object code.

function CreateNewHttpObject()    //Create an Instance of HTTP request.
 
{
    
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;
  
}
  
function ZipState(ZipCode,StateTxt)
  
{
    
var ObjHttp= new CreateNewHttpObject();
    if(
ObjHttp
    
{
      
var dataSource "<?=base_url?>test/StateByZip("+ZipCode+")";
      
ObjHttp.open("GET"dataSource);
      
ObjHttp.onreadystatechange = function()
      
{
        
if (ObjHttp.readyState == && ObjHttp.status == 200)
        
{
          
var returnVal=ObjHttp.responseText;
          
document.getElementById(StateTxt).value=returnVal;
        
}
        ObjHttp
.send(null);
      
}
    }
  }

<input name="zip" type="text" id="zip" onblur="return ZipState(this.value,'state')" value="40011" />
<
input name="state" type="text" id="state" value="state" /> 

My code first create HttpObject which works fine in many other projects (not using CI). After that it calls my test controller’s StateByZip($ZipCode) funciton. Upto this point it works fine but after that i feel, my code can’t call and execute StateByZip($ZipCode) funciton and as such can’t display state value in state box.

Please help me resolve it. Or at least send me an example “how to use CI with AJAX”.
Thanks in Advance.

 Signature 

Md. Aminul Islam
http://www.shopno-dinga.com (CodeIgnited Site)

Profile
 
 
Posted: 22 May 2008 02:23 AM   [ Ignore ]   [ # 1 ]  
Summer Student
Avatar
Total Posts:  18
Joined  12-26-2006
var dataSource "<?=base_url?>test/StateByZip("+ZipCode+")"

You are missing () for base_url() function.

By the way….you can post this thread in “Code and Application Development” as this forum is only for Ignited Code

 Signature 

Looking To Improve…

Profile
 
 
Posted: 22 May 2008 02:36 AM   [ Ignore ]   [ # 2 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

You have to remove the base_url all together because there is a same domain protection build in the xmlhttprequest object.

Profile
 
 
Posted: 22 May 2008 02:48 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  79
Joined  03-02-2008

I suggest use Prototype/Scriptaculous framework.

 Signature 

Geshan Manandhar
http://geshan.blogspot.com

Profile
 
 
Posted: 22 May 2008 02:52 AM   [ Ignore ]   [ # 4 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  303
Joined  06-25-2007

Ok i post it in http://codeigniter.com/forums/viewthread/80192/
Please ignore this post.

 Signature 

Md. Aminul Islam
http://www.shopno-dinga.com (CodeIgnited Site)

Profile