Shortened Aim: To pass a variable via $.POST to a PHP file (get.php), and for get.php to $.POST the variable again to db.php.
My Problem
I’m trying to follow the model-view-controller design pattern within my PHP code.
I’ll have three pages:
- index.php – The GUI (basically HTML and DIV tags)
- get.php – The hidden php which serves code via AJAX + POST to divs in index.php
- db.php – The database connection part
Basically, I want to receive a response through two POST requests
So far I have index.php POSTING to get.php and requesting certain data, which is returned via and updated via AJAX and jQuery for animation.
Here’s a code snippet:
function get(div,id,opt_username) {
$.post('get.php', { request: id },
function(response) {
// In this order, first fadeout div
$(div).fadeOut(600, function() {
// If we pass a username as optional parameter...
if (opt_username) {
//Prepend username onto response
response = " Welcome " + username + response;
}
// Then get the response back HIDDEN
$(div).html(response).hide(function() {
// Once completed, parse with XFBML so everything renders
FB.XFBML.parse(document.getElementById(div));
// Only after everything's parsed do we fade back in
$(div).fadeIn(1100, function() {});
});
});
});
};
Now I can do POST requests to get.php, but if I try and do the same thing within get.php to post to db.php (to perform database operations) this just does not seem to work.
My Aim
index.php -> request data -> get.php -> request db connection -> db.php -> return data -> get.php -> return data -> index.php
This will allow me to switch the database layer out with another one. For example I’m currently using RDBMS but I would like to switch to XML / RDFXML in the future, meaning all I have to do is switch out the db.php for another one and everything will still work, keeping in-line with the MVC.
My Questions
- First and foremost, can I actually make a POST to a page to make another POST and if so, why am I getting the error below?
- Am I going about the Model View Controller approach the correct way
Further Explanation
Here is a code snippet in get.php (the second page) which throws an error:
$.post('db.php', { test: 2 },
function(response) {} );
Error thrown:
Parse error: syntax error, unexpected '.', expecting T_VARIABLE or '$'
Conclusion
I simply can’t get index.php to post to get.php, in turn posting to db.php.

Implementing model-view-controller does not mean you have to create HTTP requests to communicate between these layers. Simply include the file using
includeorrequire. The seperation already exists by having different files.