CodeIgniter Forums

Full Version: Integrating jQuery & CI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]KevinSPerrine[/eluser]
I'm new to developing PHP/Ajax driven sites, and I'm having a hard time getting this little problem straightened out. I've read these posts (1,2) on loading partials and this one as a quick intro to jquery w/ CI. I'm trying to get it to load a new view in the content div when a link on the navbar is clicked, but it doesn't change anything. The home content remains in place. I've included jquery.js in the header and arvt.js right after. Firebug doesn't give me any js errors, so I think it has to do with how the view is loaded with CI. I can also change the js code to $("#content").hide(); and it gets hidden properly. Any help will be greatly appreciated.

Code:
//Controller crop.php
function index()
{
    $data['title'] = "Arkansas Variety Testing";
    $data['content'] = $this->load->view('content/home','',true);
    $this->load->vars($data);
    $this->load->view('arvt', $data);
}
    
function sorghumcorn()
{
    $this->load->view('content/sorghumcorn');
}

//View arvt.php
<?php
    $this->load->view('header');
    $this->load->view('leftnav');
    $this->load->view('content');
    $this->load->view('footer');
?>

//view content.php
<div id="content" class="span-12 last">
    &lt;?php $this->load->view('content/home'); ?&gt;
</div>

//links in navbar
<li><a href="" -onclick-="loadhome()">Home</a></li>
<li><a href="" -onclick-="loadsorghum()">Sorghum &amp; Corn</a></li>

//arvt.js
function loadsorghum(){
    $("#content").load("&lt;?=base_url?&gt;crop/sorghumcorn");
}

function loadhome(){
    $("#content").load("&lt;?=base_url?&gt;index.php");
}

El Forum

[eluser]TheFuzzy0ne[/eluser]
It's strange that FireBug doesn't show any errors. If the onclick function doesn't exist when you click on it, then it will throw an error, so I think the problem might be with your JavaScript code. In your JavaScript code, shouldn't you be calling &lt;?=base_url()?&gt; rather than &lt;?=base_url?&gt;?

El Forum

[eluser]KevinSPerrine[/eluser]
You're correct, it should be base_url(). I've changed it a few times and mistyped it last time. The method is being found because I can set a firebug break point on it and walk through the jQuery code it calls when I click the link.

El Forum

[eluser]jdfwarrior[/eluser]
um.. Smile Did you also notice that you are calling a CI/PHP function within a .js file? Have you tried passing base_url() into the function as a parameter and then using that value in your js to do the load?

edit: I could be an idiot for thinking that wont work.. I've never tried that so correct me if I'm wrong. I just know that like, for CSS you cant just echo php stuff in it. You have to make a php file, and use php to echo a content type and then have it echo all the css to make something like that work.

El Forum

[eluser]TheFuzzy0ne[/eluser]
You make a very good point. I assumed the script was being parsed by CodeIgniter first, but I shouldn't make assumptions like that.

El Forum

[eluser]jdfwarrior[/eluser]
I only caught it because he noted that his javascript was a separate file. (//arvt.js)
If it were a script that was in the view file (which would be php), the variable would have worked. Being a javascript file though, I don't think it works like that. As I said, someone correct me if I'm wrong. It's very possible :)

edit: If that still doesnt work, I would also try to make sure that your function is definitely getting called by adding something simple to the code. Like an alert("Function called"); before the load command. If the alert message works, then you know it is definitely just something with the load command.

El Forum

[eluser]KevinSPerrine[/eluser]
Thanks, I didn't realize that was a problem. However, it still didn't fix my problem. I did try removing the href tags from the links and just use anchor and onclick and it works. Can someone explain that to me?

El Forum

[eluser]jdfwarrior[/eluser]
[quote author="Kevin Perrine" date="1244680579"]Thanks, I didn't realize that was a problem. However, it still didn't fix my problem. I did try removing the href tags from the links and just use anchor and onclick and it works. Can someone explain that to me?[/quote]

May not have liked that href was blank. Could have tried it as href="#"

El Forum

[eluser]KevinSPerrine[/eluser]
I did. It didn't like that either.

El Forum

[eluser]jdfwarrior[/eluser]
Odd stuff, so it still isn't working at all? Did you try adding the alert message above the load command to make sure it is getting called?
Pages: 1 2