CodeIgniter Forums
Codeigniter and JQuery conflict? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Codeigniter and JQuery conflict? (/thread-26785.html)

Pages: 1 2


Codeigniter and JQuery conflict? - El Forum - 01-23-2010

[eluser]PODz_EN_Co.[/eluser]
I have a page written in php codeigniter with jquery. JSON seems to have a problem in locating the json function. Here's the scenario:

When you visit the site, you will be redirected to http://localhost/login. I have a test button in here to see if json is working well and here's the code:

JSON:
Code:
$('input#testBtn').click(function(){
    var item = $('input#txtUsername').val();
    $.post('login/testJSON', { 'item' : item },
      function(transport){ alert(transport.result); }, 'json');
});

CONTROLLER (under the login.php controller):
Code:
function testJSON(){
    $item = 'TESTJSON';
    $array = array('result' => $item);
    echo json_encode($array);
}

The above code works well if you are in the index of the login (http://localhost/login) however, if you have an invalid password you'll be redirected to http://localhost/login/authenticate which is the same page as the login index and if you try the test button for json, it will not work anymore.

Any ideas?


Codeigniter and JQuery conflict? - El Forum - 01-23-2010

[eluser]flaky[/eluser]
have you tried debugging with firebug?


Codeigniter and JQuery conflict? - El Forum - 01-23-2010

[eluser]srisa[/eluser]
Test it with absolute urls and as mentioned earlier, use firebug.


Codeigniter and JQuery conflict? - El Forum - 01-23-2010

[eluser]PODz_EN_Co.[/eluser]
i'll try that and will post results later.


Codeigniter and JQuery conflict? - El Forum - 01-23-2010

[eluser]PODz_EN_Co.[/eluser]
works well with absolute but not a good idea when u have to move the site to other domain.


Codeigniter and JQuery conflict? - El Forum - 01-23-2010

[eluser]PODz_EN_Co.[/eluser]
i've tried adding '/' (/login/testJSON) and still doesn't work.


Codeigniter and JQuery conflict? - El Forum - 01-23-2010

[eluser]flaky[/eluser]
for absolute url use
Code:
base_url();



Codeigniter and JQuery conflict? - El Forum - 01-23-2010

[eluser]PODz_EN_Co.[/eluser]
^can i use this inside the javascript?

error console says:
"Error: base_url is not defined
Source File: http://localhost/projects/podz_en/js/authenticate.js
Line: 22"


Codeigniter and JQuery conflict? - El Forum - 01-23-2010

[eluser]flaky[/eluser]
yes you can
put this in the header before using any javascript
Code:
<scrpt type="text/javascript">
var base_url = '&lt;?php echo base_url(); ?&gt;';
</scrpt>

and inside your javascript use the variable "base_url"

P.S
instead of scrpt I meant script but the forum is stripping it


Codeigniter and JQuery conflict? - El Forum - 01-23-2010

[eluser]PODz_EN_Co.[/eluser]
^thanks! that solves the mystery! hehehe