CodeIgniter Forums
Return values from CI controllers to Javacript (jQuery) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Return values from CI controllers to Javacript (jQuery) (/showthread.php?tid=28052)

Pages: 1 2


Return values from CI controllers to Javacript (jQuery) - El Forum - 02-28-2010

[eluser]MungeGerard[/eluser]
Hi all,

I'm building my 1st website with CI and I must say, it's really great!

At this point I'm programming an Ajax(jQuery) / Controller combination to submit my forms. But I just can't figure out how to return a value from the codeigniter controller to the javascript function that calls the controller.

Javascript function
Code:
$.ajax({
    type: "POST",
    url: sUrl,
    data: sData,
    success: function(response) {
        alert(response);
    }
});

CI controller
Code:
function send() {
    if($this->session->userdata('session_id')==$_POST['sVerification']){
        if($this->email->send()){
            $result = 'send';
        }
        else {
            $result = 'error';
        }
    }
    else {
        $result = 'sessionerror';
    }
    print_r($result);
    return($result);
}

I summarized the scripts, I only included the lines that matter Smile

Everything works fine, only no data gets returned to the javascript.

Hope you guys can help me out here,

Gerard


Return values from CI controllers to Javacript (jQuery) - El Forum - 02-28-2010

[eluser]flaky[/eluser]
instead of
Code:
return($result);
use
Code:
echo $result;



Return values from CI controllers to Javacript (jQuery) - El Forum - 02-28-2010

[eluser]MungeGerard[/eluser]
Thanks for the help, but still no data gets returned..


Return values from CI controllers to Javacript (jQuery) - El Forum - 02-28-2010

[eluser]haydenk[/eluser]
Is it your javascript is getting no data or the PHP script is not outputting data? Also, you don't need to print_r($result) AND return or echo $result.


Return values from CI controllers to Javacript (jQuery) - El Forum - 02-28-2010

[eluser]MungeGerard[/eluser]
[quote author="haydenk" date="1267404210"]Is it your javascript is getting no data or the PHP script is not outputting data? Also, you don't need to print_r($result) AND return or echo $result.[/quote]

It is my javascript that doesn't get any data. And the print_r was just for debugging. Smile


Return values from CI controllers to Javacript (jQuery) - El Forum - 02-28-2010

[eluser]haydenk[/eluser]
I understand, I do the same. Wink

If the PHP script is outputting the information properly, then it's all in the jQuery but the print_r could be throwing off the javascript's ability to process properly.

I'm a Mootools developer, so I couldn't tell you much about jQuery, sorry.


Return values from CI controllers to Javacript (jQuery) - El Forum - 02-28-2010

[eluser]MungeGerard[/eluser]
[quote author="haydenk" date="1267404974"]I understand, I do the same. Wink

If the PHP script is outputting the information properly, then it's all in the jQuery but the print_r could be throwing off the javascript's ability to process properly.

I'm a Mootools developer, so I couldn't tell you much about jQuery, sorry.[/quote]

Thanks for the help, unfortunately, removing the print_r didn't help much!


Return values from CI controllers to Javacript (jQuery) - El Forum - 02-28-2010

[eluser]nelson.wells[/eluser]
I like returning data from ajax calls with JSON. It's easy to use in php and in jQuery. Your php code would look like this

Code:
function send() {
    if($this->session->userdata('session_id')==$_POST['sVerification']){
        if($this->email->send()){
            $data['result'] = 'send';
        }
        else {
            $data['result'] = 'error';
        }
    }
    else {
        $data['result'] = 'sessionerror';
    }
    
    $json = json_encode($data);
    echo $json;
}

and then you could have jQuery code like this

Code:
$.ajax({
    type: "POST",
    url: sUrl,
    data: sData,
    success: function(response) {
        if(response.result == "send")
        {
             alert("sent successfully");
        }
        else
        {
              //etc.
        }
    },
    'json'
});

I hope you see how that works. This is untested since I did it in the browser, if you give it a try and something doesn't work correctly let me know and I'll help debug it.


Return values from CI controllers to Javacript (jQuery) - El Forum - 02-28-2010

[eluser]Twisted1919[/eluser]
Exit the script with result .
This way you will keep your compresion enabled without problems .
Code:
if( blah == blah ){
exit('SUCCESS');
}
exit('ERROR');



Return values from CI controllers to Javacript (jQuery) - El Forum - 03-01-2010

[eluser]MungeGerard[/eluser]
Quote:I hope you see how that works. This is untested since I did it in the browser, if you give it a try and something doesn't work correctly let me know and I'll help debug it.

Thanks for all the help, but the exit method didn't work. I looked at JSON and it seems promising, I found an example and did exactly the same.

Javascript
Code:
var sUrl = ($("input#url").val() + "index.php/contact/send");
var sName = $("input#name").val();

$.post(sUrl, { 'item' : sName },
    function(data){
    alert(data.result);
}, "json");

Controller
Code:
function send() {
    return($result);
    $item = '1234';      //trim($this->input->post('item'));
    $array = array('result' => $item);
    echo json_encode($array);
}

Still no data gets returned to the javascript. When I call the contoller/function directly I get the following output:
Code:
{"result":"1234"}

So do you have any ideas?