1

It's my first post in here and I haven't yet figured out to format my post properly yet, but here it goes.

So basically I can only get my code to work if i point directly to a php-file. If I try to call a method within my controller, nothing seems to happen.

My JavaScript:

$(document).ready(function() {  
$(".guide_button").click(function(){

    var id = $(this).text();
    var data = {};
    data.id = id;

    $.getJSON("/guides/hehelol", data, function(response){
        $('#test').text(response.id);
    });
    return false;
});
});

My markup:

<div id="content_pane">
<ul>
  <li><a href="#" name="temp" class="guide_button">RL</a></li>
  <li><a href="#">LG</a></li>
  <li><a href="#">RG</a></li>
  <li><a href="#">SG</a></li>
  <li><a href="#">GL</a></li>
  <li><a href="#">MG</a></li>                       
</ul>                   
</div>

<div class="description">
<h3>Description</h3>
<p id="test">This text area will contain a bit of text about the content on this section</p>
</div>

My Controller:

<?php
class Guides extends CI_Controller {    

public function Guides()
{
    parent::__construct();

    $this->load->helper('url');
    $this->load->helper('form');
}   

public function index()
{
    $this->load->view('guides_view');
    $title = 'Some title';      
}   

public function hehelol() //The controller I am desperatly trying to call
{   
    $id = $_GET['id'];       
    $arr = array ('id'=>$id);
    echo json_encode($arr);
}           
}

It might be my controller I have done something wrong with. As it is the code only works if create a hehelol.php file and refer to it directly like this.

$.getJSON("hehelol.php", data, function(response){
$('#test').text(response.id);
});

Anyone who knows what I need to do to make my controller work properly? Help please! :)

Shai
  • 111,146
  • 38
  • 238
  • 371
user1390322
  • 61
  • 1
  • 3
  • 1
    Have you tried using a complete url instead of a relative one (http://localhost/index.php/guides/hehelol)? I find that that resolves 93.7% of my problems. – J.T. Grimes May 11 '12 at 23:22
  • Probably a routes issue, mind posting your routes? – deex May 11 '12 at 23:35

1 Answers1

0

i just put your exact code in its entirety in my codeigniter app and it worked for me. Meaning I used this: ...$.getJSON("/guides/hehelol",...

Because you are making a $_GET request, you have to enable query strings.

In your config.php file, make sure this line is set to TRUE:

$config['allow_get_array']= TRUE;
tim peterson
  • 23,653
  • 59
  • 177
  • 299
  • I just checked my config.php and unfortunatly it is already set to true per default. Obviously you must have some other settings that allow you to run the code correctly that I dont have. – user1390322 May 12 '12 at 00:48
  • @user1390322 can you say what your `response` is when using this url, `$.getJSON("/guides/hehelol",`? – tim peterson May 12 '12 at 00:53
  • Well just nothing seem to happen at all, so I dont even get a response.When I click the chrome console tells me this: GET http://localhost/guides/hehelol?id=RL 404 (Not Found) – user1390322 May 12 '12 at 09:28
  • @user1390322, I mean the response that can be shown in your browser's developer tools, have you used those with chrome for example? also if you put a console.log('hello') above the return false; does it show in the console? – tim peterson May 12 '12 at 12:26
  • The response in the developer tool reads: {"id":"RL"} And yes the hello shows up fine in the console. – user1390322 May 12 '12 at 12:37
  • @user1390322 What would you like your output to be? changing the text inside `

    `, correct? Is clicking the first link not even changing `$('#test).text()` to "RL"? If that works ok, then to change the others, you have to put the `class="guide_button"` with each `` to make your `click()` function recognize them.
    – tim peterson May 12 '12 at 13:02
  • @user1390322, is that response, `{"id":"RL"}` what you want? If, so this is solely a jQuery/javascript issue and your codeigniter/PHP is fine. – tim peterson May 12 '12 at 13:06
  • For now I just want my view to communicate with my controller properly. To do this I just wanted the view to pass the text of the button to a variable in the controller which then should return it to the #test just to know that it worked. I figured it should be faily simple to make that work. As soon as I have that actually working I'm gonna let it fetch something from my database and return that to the view in stead. But first things first, since I cannot even pass the text back and forth between the view and controller at this point. – user1390322 May 12 '12 at 13:12
  • @user1390322 to me, your communication with your controller is working. Can you explain to me why you think it is not working? – tim peterson May 12 '12 at 13:14
  • It has to be something with the URL in the json im guessing. As mentioned if i call a hehelol.php with the same code as in the controller, then it works just fine. But since I'm using CodeIgniter, want to be able to call the hehelol-method in my controller, in stead of a php-file. – user1390322 May 12 '12 at 13:19
  • the codeigniter-style URL is working if the `response` is good. Since you say the response is good the problem is not the URL. – tim peterson May 12 '12 at 13:25
  • yeah, the URL is fine, the response is fine. Can you explain one more time why you think there is a problem? From my view, you are getting no errors and everything is working as it should. – tim peterson May 12 '12 at 13:28
  • Yes but the text in #test does not change at all when I click the button as it should. Also in the console it says GET localhost/guides/hehelol?id=RL 404 (Not Found) – user1390322 May 12 '12 at 13:34
  • @user1390322 something isn't matching up. You can't get a correct `echo json_encode($arr);` to produce `{"id":"RL"}` if `localhost/guides/hehelol` doesn't exist. You must have changed something between the time you got `{"id":"RL"}` and now in order for you to be getting a 404. – tim peterson May 12 '12 at 13:38
  • That might be true yes. In the firebug console I get the GET localhost/guides/hehelol?id=RL 404 (Not Found) error message. If I click it there is then a pane called "Response" which contains {"id":"RL"}. – user1390322 May 12 '12 at 13:45
  • @user1390322 there is no way to get those both at the same time. the "response" must be referring to an old response when things were working correctly, its just that you didn't notice they were (or you had changed the JS so that it wasn't been shown on screen). Can you go back and copy/paste your HTML/JS/PHP from this question over what you have now? I just did it again myself and it works fine. I still don't see the problem, my `#test` text changes to "RL" just fine. – tim peterson May 12 '12 at 13:54
  • I have exactly what I posted in the question, but nothing happens when I click the button. The text in #test does not change to RL as it should. Just nothing happens, and in the console gives me that error message. – user1390322 May 12 '12 at 13:57
  • @user1390322 can you clear the old responses and show me what the response is just for the current button click? – tim peterson May 12 '12 at 14:01
  • do you want to skype to resolve this? if so my username is petersti1 – tim peterson May 12 '12 at 14:02