Welcome Guest, Not a member yet? Register   Sign In
question of ajax and CI
#1

[eluser]piranha_swe[/eluser]
Hi!

Todays questions is about ajax and CI :-).

When I have a php file that is loaded using jquery, it seems like it will be treated like an ordinary php file that is not part of CI. Is this the case?
Or can you use CI models and libraries on your ajax file aswell? How can I do that?

Seems a bit wastish to have a second set of files that doesnt use CI. Like database classes and template classes and so on.
#2

[eluser]pistolPete[/eluser]
You can of course use CI in an ajax request, why not?
Controllers, Models and Views are used the same way as in a normal request.
Do you have any specific question / code?
#3

[eluser]piranha_swe[/eluser]
I have this code to load a shopping cart

function BuyRecord(id)
{
$("#cart").load("<?php echo $base;?>ajax/ajax_cart.php", { 'recordid':[id] });
}

Then in the ajax_cart.php file I have this at the beginning

$this->load->model(array('Datawork', 'Template', 'Form', 'Process'));
$recordid = utf8_decode($_POST['recordid']);

$recorddata = $this->Datawork->DBGet("records", array(id => $recordid), "", "", "");

I get this result
Fatal error: Using $this when not in object context in ...vinylcatch.se/ajax/ajax_cart.php on line 2

I'm just unsure what I should do, There is no mentioning the ajax_cart file in my controller. I shouldnt load it as a view??
#4

[eluser]pistolPete[/eluser]
You need to create a function in your controller which is accessed using ajax, e.g. :

Code:
class Your_controller extends Controller {

...
    
    public function ajax()
    {
        $this->load->model(array('Datawork', 'Template', 'Form', 'Process'));
        $recordid = utf8_decode($_POST['recordid']);
        $recorddata = $this->Datawork->DBGet('records', array(id => $recordid), '', '', '');
        ...
    }
...

}

Then in the view, access the function using:

Code:
function BuyRecord(id)
{
    $('#cart').load('/your_controller/ajax', { 'recordid':[id] });
}

Have a look at some tutorials about ajax and CI:

http://www.mrforbes.com/thoughts/2009/01...-tutorial/
http://littlebrain.org/2008/05/27/codeig...-tutorial/
http://geekhut.org/2009/06/how-to-codeig...uery-json/
http://ellislab.com/forums/viewthread/71636/
#5

[eluser]piranha_swe[/eluser]
I've been trying like you say now but I'm not getting it working. I still find the URL a bit unclear at times, like now.

I tried a bunch of diffrent ways, and most just dont give me any result but this will give me a weird result:

{
$('#cart').load('/records/ajax_cart', { 'recordid':[id] });
}


public function ajax_cart()
{
echo "adsf";
}

It will load the whole page again ontop of the other page. I use some transparent images on the site so I can see my site loaded a few pixels moved ontop of the first one. Any idea what I could be doing? or what I should be doing instead?

To try what I mean: go here: http://www.vinylcatch.se/records/house and click Add to Cart
#6

[eluser]pistolPete[/eluser]
If I access http://www.vinylcatch.se/records/ajax_cart directly, I get the whole page instead of the "asdf" message.

Are you sure the function ajax_cart() is in the records controller?
What routes do you use?
Do you use the _remap() function?
If possible, post the whole records controller.

And please wrap [ code ] tags around your code samples!
#7

[eluser]piranha_swe[/eluser]
Yes its located in that controller, its the same controller as the rest of the public site, should I put it in another controller?

Its alot of code, but the main page is located in the index function.

<?php
class Records extends Controller {

function index()
{
.... lots of code ...
}
Then I have functions for pages on the menu, how to, contact, news letter and so on that looks like this
function faq()
{
//LOADING
$this->included();

$table['pagetext'] = $this->Datawork->DBGet("texts", array('id = 7'), "", "", "");

$this->load->view('css/style');
$this->load->view('top');
$this->load->view('faq', $table);
$this->load->view('bottom');
}

. Then at the end of the file I have the ajax_cart function.

I havent used the _remap function, I think I seen it somewhere in the guide or on this forum, but I dont know what it is.
I use a .htaccess file to remove index.php, I also try to remove the records/ part from the url, but I'm not sure what I've done is correct. My admin part is not working correct at the moment with this .htaccess file.
Looks like this

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(css=.*)$ [NC]
RewriteRule ^(.*)$ /index.php?/records/%1 [L]
RewriteCond $1 !^(images|system|photos|sounds|ajax|favicon/.ico|robots\.txt|index.php) [NC]
RewriteRule ^(.*) /index.php?/records/$1 [L]


I get the same result as you with the link http://www.vinylcatch.se/records/ajax_cart.
#8

[eluser]pistolPete[/eluser]
You didn't tell me if you use any routes!? http://ellislab.com/codeigniter/user-gui...uting.html

And please wrap [ code ] tags around your code samples!
#9

[eluser]piranha_swe[/eluser]
Hi, I really needed that weekend from fighting with CI :-).

I'm not using any routing if you mean his?

Code:
$route['default_controller'] = "";
$route['scaffolding_trigger'] = "";

I changed my routing to records
Code:
$route['default_controller'] = "records";
#10

[eluser]piranha_swe[/eluser]
Hm its working now that I changed my htaccess file.

How should my htaccess file look if I want to remove index.php and the controller records from the visible url?

This works for the ajax call:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(css=.*)$ [NC]
RewriteRule ^(.*)$ /index.php?/%1 [L]
RewriteCond $1 !^(images|system|photos|sounds|ajax|favicon/.ico|robots\.txt|index.php) [NC]
RewriteRule ^(.*) /index.php?/$1 [L]

But I would like to remove /records too.
This seems to be bad since the ajax thing doesnt work, and admin doesnt work either.

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(css=.*)$ [NC]
RewriteRule ^(.*)$ /index.php?/records/%1 [L]
RewriteCond $1 !^(images|system|photos|sounds|ajax|favicon/.ico|robots\.txt|index.php) [NC]
RewriteRule ^(.*) /index.php?/records/$1 [L]




Theme © iAndrew 2016 - Forum software by © MyBB