Good day!
I'm learning to create AJAX calls to PHP scripts.
Basically, I want to find the best solution to handle AJAX calls. In this question you can find my client-side part of the code.
Basically, I'd like to verify my knowledge here and if I'm wrong, get any tips how to make it correct way.
So... I'm using KohanaPHP framework (but also going to learn ZendFramework). I created a test controller with following code:
public function __construct()
{
if (request::is_ajax()) {
$this->auto_render = FALSE;
header('content-type: application/json');
}
}
public function index()
{
$result['success'] = 1;
$test_model = new Test_Model;
$items = $test_model->get_test_rows();
foreach($items as $item):
$rows[] = $item;
endforeach;
$result['rows'] = json_encode($rows);
if (request::is_ajax()) {
echo json_encode($result);
}
}
Now I got few questions related to this code. Let me ask them.
1) In thread mentioned above I was told I do not have to use $.parseJSON();
function and I can use data.rows[0]name
instead. Unfortunately, it does not work. For the is't not a big problem, I can use $.parseJSON();
but I'd like to know if there's any bug in my PHP code?
2) I'm trying to learn good practices and techniques and I'm try to not violate MVC rules. I have a MVC related question. Is it possible to use echo()
in controllers? You may notice I'm using it to return JSON encoded data.