CodeIgniter Forums
basic ajax using CI - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: basic ajax using CI (/showthread.php?tid=10533)

Pages: 1 2


basic ajax using CI - El Forum - 08-03-2008

[eluser]misterdonut[/eluser]
how can i use xml http request using CI?

the wiki is not giving enough info on how to use it.

any links or resources?

the video of derek was good but didn't explain the structure and installation of his video.
so i'm quite confused.

please help


basic ajax using CI - El Forum - 08-03-2008

[eluser]Colin Williams[/eluser]
Well, an Ajax request is typically expecting either XML or JSON in return. So all you need to do is return XML or JSON from the requested Controller method. There's really nothing more to it from a high level.

Code:
class User extends Controller {

function username_complete()
{
   $names = $this->user_model->get_names($this->input->post('username'));
   $this->load->view('xml/usernames', array('names' => $names));
}

}

You could imagine the xml/usernames.php view returning something like

Code:
<names>
   <name>Johno</name>
   <name>Johnimite</name>
</names>



basic ajax using CI - El Forum - 08-03-2008

[eluser]Pascal Kriete[/eluser]
EDIT: Colin again! :lol:

Hey,

AJAX works the same as it would with any server side language. Your javascript calls a url, and the server returns a bunch of text.

So let's say you have a controller: woot, with a function: do_ajax:
Code:
function do_ajax()
{
    echo 'test';
    exit;
}

Now your ajax request would look something like this, depending on the implementation:
Code:
url = 'http://example.com/woot/do_ajax';

new Ajax.Request(url, {
    type: GET,
    success: function(response) {
        alert(response.responseText);
    }
});

Does that help?


basic ajax using CI - El Forum - 08-03-2008

[eluser]misterdonut[/eluser]
its not working.

Quote:$('my_form').observe('submit', function(evt) {

evt.stop();

var url = '&lt;?=base_url()?&gt;index.php/specialist/changediv';

var container_div = $('wrapper');
var form_data = $('my_form').serialize();

new Ajax.Request(url, {
method: 'post',
parameters: form_data,
onCreate: function() {
container_div.update('Loading....');
},
onSuccess: function(transport) {
container_div.update(transport.responseText);
}
});
});

Code:
<div id="wrapper">
&lt;form action="&lt;?=base_url()?&gt;index.php/specialist/changediv" method="post" id="my_form" accept-charset="utf-8"&gt;
    &lt;input type="submit" value="getValue"&gt;
&lt;/form&gt;
</div>

<div id="wrapper">
</div>

it wont end on loading

the change div has
Code:
function changediv(){
    echo "changed div";
}



basic ajax using CI - El Forum - 08-03-2008

[eluser]Randy Casburn[/eluser]
Please post the contents of your $_POST array and your specialist controller.

Randy


basic ajax using CI - El Forum - 08-03-2008

[eluser]misterdonut[/eluser]
ok i am just trying to get the changed div.
but i'm not using any value in my post array;

i just use that event for execution.
as i am beginner in prototype.

so i really need to get values from my controller.

a complete example would be great.


basic ajax using CI - El Forum - 08-03-2008

[eluser]Randy Casburn[/eluser]
Ok...that's what I was trying to confirm. Forget the $_POST array...

Can you post your controller code at least so we can take a look at what you're trying send back as a response?

Thanks,

Randy


basic ajax using CI - El Forum - 08-03-2008

[eluser]misterdonut[/eluser]
Code:
function changediv(){
    echo "changed div";
}

a simple echo from my specialist controller


basic ajax using CI - El Forum - 08-03-2008

[eluser]Randy Casburn[/eluser]
misterdonut....is this actually what is in your HTML source?

&lt;form action="&lt;?=base_url()?&gt;index.php/specialist/changediv"


or is that in a View file? It would be more helpful if you would label what you are posting here. Otherwise you may just be wasting a lot of time.

If that is what is actually in your HTML source (from your browser), then it should be clear that this part "&lt;?=base_url()?&gt;" can't be in your HTML. That needs to be fixed or your Ajax request won't be posted to the right URL.

Randy


basic ajax using CI - El Forum - 08-03-2008

[eluser]misterdonut[/eluser]
its on my php.

it was translated the right way.

Code:
<div id="form_wrapper">
&lt;form action="http://a-it03/HotelNew/index.php/specialist/changediv" method="post" id="my_form" accept-charset="utf-8"&gt;
    &lt;input type="submit" value="Change Value"&gt;
&lt;/form&gt;
</div>

and i can access http://a-it03/HotelNew/index.php/specialist/changediv through my browser.