Javascript call from controller result possible?

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

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

#3
[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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.