Reputation: 26624
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:
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
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.
Upvotes: 1
Views: 785
Reputation: 5377
Implementing model-view-controller does not mean you have to create HTTP requests to communicate between these layers. Simply include the file using include
or require
. The seperation already exists by having different files.
Upvotes: 1
Reputation: 10646
Do you have to make two post
s?
I don't know what your files look like, but right now I'm guessing that they are all regular php files which you are duplicating for each of your pages when you need to implement that page's functionality, so my answer will try to fit into that but as I said, I don't know what your files actually look like so this may not fit into the way you are working.
That said, based on the way that I think you are using your files this is what I would suggest.
Instead of trying to make two posts between the pages, why not just pass the $_POST array to a method in a class?
For example (In pseudo code):
Your index.php (posts to get.php via AJAX):
<form action="/uses/ajax/to/post/to/get.php" method="post" name="data_form" id="data_form">
<input type="text" name="data" id="data" />
<input type="text" name="more_data" id="more_data" />
<input type="submit" value="Post this data" />
</form>
<script>
// Your ajax post code here
</script>
Your get.php:
<?php
$post_data = isset($_POST) ? $_POST : die('No post data was sent');
include($_SERVER['DOCUMENT_ROOT'] . '/Model_Object.php');
$Model_Object = new Model_Object();
// This would echo your response for the ajax
echo $Model_Object->create_something($post_data);
Then instead of having just db.php you can create a Model class (which is included in your get.php):
Model_Object.php:
<?php
class Model_Object {
public function create_something($post_data) {
$data = isset($post_data['data']) ? $post_data['data'] : '';
$more_data = isset($post_data['more_data']) ? $post_data['more_data'] : '';
// MySQL query here
$success = true;
if($success) {
return 'Completed';
}
return 'Failed';
}
public function create_something_else($post_data) {
return 'This can do a different MySQL query';
}
}
It's not the best way of doing it, but given your explanation of your own code and based on the way I think your code is working this might be a solution for you.
As for going about the MVC approach in the correct way, I don't know if this is how it is best done but, when I write MVC applications from scratch I setup a .htaccess
file to route all requests to the root index.php
, which would, from the URI decide which Controller
object to create and which method within that Controller
to execute.
That method might for example contain some code to display a view, or create a Model
object to interact with the database.
If a Model
were to be created then it would pass back any relevant information to the Controller
and the Controller
would do what it needs to do from there.
For example if I went to /my_controller/my_action
then the index.php file would create the My_Controller
object if it existed, and if it existed try to execute a method within that object called my_action
.
I hope this has been of some help to you and I'm sorry if I have mis-understood.
Upvotes: 2