Welcome Guest, Not a member yet? Register   Sign In
Return values from CI controllers to Javacript (jQuery)
#1

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

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

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

[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.
#5

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

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

[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!
#8

[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.
#9

[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');
#10

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




Theme © iAndrew 2016 - Forum software by © MyBB