0

I have a problem with AJAX request in PHP MVC framework: I don't know how to call JSON formatted data from controller to the view using, for example, jQuery.

I wasted many hours searching for any useful tip on the internet and trying to resolve this problem by myself but with no success.

I think the problem is where I need to write the URL; I can't write it correctly.

This is the code for the controller:

public function indexAction()
{
   $dbh = new PDO('mysql:dbname=myframework;host=localhost', 'root', '');
   $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   $stmt = $dbh->prepare('SELECT variety ,fruit_id FROM fruit limit 10');
   $stmt->setFetchMode(PDO::FETCH_ASSOC);
   $stmt->execute();
   $data = $stmt->fetchAll();
   echo json_encode($data);
}

And this is the view code:

<!DOCTYPE html>
<html><head><meta charset="utf-8" /> </head>
 <h1>grid ajax</h1> <body>  <table id='grid'></table>
<script type="text/javascript" src="<?php echo PROJECT_URL ?>/views/search/jquery.js"></script>
<script type="text/javascript">
$(function(){
$.getJSON( myURL - HERE IS PROBLEM ! , function(json){
 for (var i=0;i<json.length;i++) {
 $('#grid').append("<tr><td>" + json[i].fruit_id + "</td><td>" + json[i].variety + "</td></tr>")
       }
    });
 });
</script>
</body>
</html>

Here is routing:

getUrlFor($controller = 'index', $action = 'index') { $route = array('controller' => $controller, 'action' => $action); $routes = $this->getRoutes(); $url = array_search($route, $routes); if ($url === false) { $url = $controller . '/' . $action; } $url = PROJECT_URL . '/' . $url; return $url; } }

and public function urlFor():

public function urlFor($controller = 'index', $action = 'index')
{
    return Router::getInstance()->getUrlFor($controller, $action);
}

This function works in this framework everywere; URL form example: http://localhost/myframework/ajax/index, (var $myURL = "<?php echo $this->urlFor('ajax', 'index'); ?>"; - in jQuery I use it in this form), only not in $.getJSON. I tried to write this URL in many forms, none of them worked. It is simple custom framework.

6
  • how do you write url in that framework? also, your <h1> tag is outside <body> tag, and your <table> ends as soon as it begins; I strongly recommend to validate your html there Commented Nov 11, 2011 at 21:49
  • var $myURL = "<?php echo $this->urlFor('ajax', 'index'); ?>"; in this form Commented Nov 11, 2011 at 21:58
  • function from router: public function getUrlFor($controller = 'index', $action = 'index') { $route = array('controller' => $controller, 'action' => $action); $routes = $this->getRoutes(); $url = array_search($route, $routes); if ($url === false) { $url = $controller . '/' . $action; } $url = PROJECT_URL . '/' . $url; return $url; } } Commented Nov 11, 2011 at 22:10
  • don't post code in comments, is difficult to read; update your question instead! Also, you didn't say if this is a custom framework or what? (Kohana?) Commented Nov 11, 2011 at 22:28
  • ok sorry Damien, it is custom framework Commented Nov 11, 2011 at 22:51

1 Answer 1

1

I'm not sure how your routing is setup so can't offer specific help. But what I usually do is (I use codeigniter) make a separate controller for all ajax calls. The functions in the controller gets the data from the model, passes that data to the view and servers the view to the ajax request.

Codeigniter has (imo) very nice routing, you call the ajax controller with the url

http://sitename.com/index.php/controller_name/function

and that's what you pass the the ajax call

$.getJSON( 'http://sitename.com/index.php/controller_name/function' , function(json){
 for (var i=0;i<json.length;i++) {
 $('#grid').append("<tr><td>" + json[i].fruit_id + "</td><td>" + json[i].variety + "</td></tr>")
       }
    });

Hope this helps at all.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks Zelijko but it is not Codeigniter, i know in Codeigniter this thing is simple, but here is problem, routing is here: public function getUrlFor($controller = 'index', $action = 'index') { $route = array('controller' => $controller, 'action' => $action); $routes = $this->getRoutes(); $url = array_search($route, $routes); if ($url === false) { $url = $controller . '/' . $action; } $url = PROJECT_URL . '/' . $url; return $url; } }
Can you post a sample url from this site. Just so I can figure out the url structure. Looking at getUrlFor() it looks to me the url maybe similar to codigniter, projecturl/controller/action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.