Welcome Guest, Not a member yet? Register   Sign In
AJAX output w/o using echo
#1

[eluser]flackend[/eluser]
Is there a way to return data to your view (using AJAX) from the controller without using echo?

Code:
function getsomething() {

    $this->load->model('data');

    // Is there a way to return data
    // without using echo?
    echo $this->data->querySomething($this->input->post('something'));
}

Code:
$.ajax({
    type: 'POST',
    data: {
        'something': something
    },
    url: base_url +'/controller/getsomething',
    success: function(data) {

        // Also, why does '0' evaluate
        // to TRUE without the
        // parseInt()?
        if(parseInt(data))
            conosole.log(data);
    }
});
#2

[eluser]LuckyFella73[/eluser]
I don't think so. RETURN doesn't work afaik.

Why don't you like to use "echo" here? Just curious
#3

[eluser]flackend[/eluser]
My understanding of MVC is that it's bad practice to echo anything from your controller. Plus I want keep all my data exchange as private as possible.

And what if you want to return an object like an array?
#4

[eluser]LuckyFella73[/eluser]
Quote:My understanding of MVC is that it’s bad practice to echo anything from your controller.
In PHP / MVC context it would be bad practice but thats the way you get values
via ajax request. In that case it's not bad practice - at least in my understanding.

Quote:Plus I want keep all my data exchange as private as possible.
Thats a good point where I dindn't find a good solution either (but didn't
do much research yet Wink ). Maybe you can use tokens somehow to prevent direct access
to you method from "evil-requests".

Quote:And what if you want to return an object like an array?
In that case you can return/echo json data or xml. In my experience it's
allways good to send an appropriate header - setting charset and/or
content type (when returning xml at least).
I just had a charset problem in google chrome when returning html without
sending a utf-8 header the same run...
#5

[eluser]flackend[/eluser]
Thanks!
#6

[eluser]Aken[/eluser]
I always recommend setting output via the Output class, instead of echoing directly. That way, if you want to turn on settings such as output compression, it won't throw errors.
#7

[eluser]flackend[/eluser]
Before starting this thread, I read in another forum post that the output class isn't as quick as exit().

I'll have to look into output compression.

Also...

Code:
echo 0;     // TRUE
echo FALSE; // FALSE
exit(0);    // FALSE

But as far as I know you can't use exit and the terinary operator:

Code:
echo $this->data->updateSomething($data) ? 1 : 0;
#8

[eluser]InsiteFX[/eluser]
Code:
$test = TRUE;
  
($test === TRUE) ? exit() : FALSE;
#9

[eluser]flackend[/eluser]
[quote author="InsiteFX" date="1327701717"]
Code:
$test = TRUE;
  
($test === TRUE) ? exit() : FALSE;
[/quote]

Cool, so:

Code:
($evaluate) ? exit('It was true!') : exit('False!')

Thanks! I didn't realize you could evaluate with parentheses like that.
#10

[eluser]CroNiX[/eluser]
The parenthesis are just there for syntax. They're not needed, but look better as it's easier to see what's going on. If php sees a ? it assumes whatever to the left is an evaluation using the ternary operator.




Theme © iAndrew 2016 - Forum software by © MyBB