CodeIgniter Forums

Full Version: Javascript call from controller result possible?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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?

El Forum

[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 :$

El Forum

[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