CodeIgniter Forums
Javascript call from controller result possible? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Javascript call from controller result possible? (/thread-3194.html)



Javascript call from controller result possible? - El Forum - 09-15-2007

[eluser]Robb__[/eluser]
I have tried a thing to control javascript actions:

Code:
var myAjax = new Ajax.Request('www/index.php/test/anAjaxCall/',{onComplete:test_response});

Code:
function anAjaxCall() {
$result ="testIfWorks('yes');";

return result;
}

in test_response
Quote:function test_response(){
eval(response.responseText)
}

Code:
function testIfWorks(text){
if (text == "yes") {alert('yep, that did the job')}
}

Is there something missing or is it not possible?


Javascript call from controller result possible? - El Forum - 09-15-2007

[eluser]Sector[/eluser]
[quote author="Robb__" date="1189879186"]
Code:
function anAjaxCall() {
$result ="testIfWorks('yes');";

return result;
}
[/quote]

Why don't you try something like:

Code:
function anAjaxCall() {
   $result ="testIfWorks('yes');";

   echo $result;
}

Because I think that returning your text doesn't do much :$


Javascript call from controller result possible? - El Forum - 09-17-2007

[eluser]obiron2[/eluser]
the javascript is running client side.

if you are doing

Code:
function testIfWorks(text){
if (text == "yes") {alert('yep, that did the job')}
}

in your controller/model this is running server side and will do nothing. If you are doing it client side in the #script# tags then text has not been set becuase "return $resul" is only adding $result to the CI object, it is not actually sending it via HTTP. You still need to generate the output using an echo, print or $this->view('your_view',$data) construction.

hope this helps

obiron