views:

64

answers:

4

Currently, I am using my own MVC framework. What I'm trying to do is make an Ajax call with jQuery(got that part down), and call on a method from the controller so that I can pass the get variables to it and get the output I want returned from the Ajax call.

I can't refresh the page because it is a page full of forms and I don't want each to be reset on a refresh. I also can't pass it to a "handle.php" where I could have the function I need, because what would be the point of the Model and Controller?

What can I do? Or rather, more importantly, what is the best way to approach such a thing? Can I load the classes I need again (would need about 6 because of the extends I use) and instantiate a new object which calls the appropriate method?

I guess it is my own fault for trying to build my own MVC framework instead of learning something like Zend, but whats done is done. Any help would be appreciated, just need to be pointed in the right direction.

A: 

From your question it's a bit difficult to find out what kind of data you're trying to get. Do you need to get data to fill the forms? Or do you need to pass data from the forms back to the database using your PHP script?

In both cases, i guess a 'handle.php' script would probably work fine. Just make some kind of bare-bones script that can handle your incoming data, validate it and load up the appropriate classes (maybe using __autoload) to save it in the database.

Husky
Basically, I have a page named playground. I want it to have all kinds of stuff I'm either testing or playing around with. Like one form is a random word generator, while another takes text to apply a gradient from one end of the string to another. Just random stuff that has little practical use.But I want to use ajax because I don't want to reset the forms, and I have plans in the future to have some forms take the innerhtml of the output of previous forms. Also, don't want to use sessions for that.
NeoTubNinja
A: 

You would more than likely want to make a request to a page much the same way you would call any other controller/method combination. For example, suppose you are trying to call controller Ajax and method Update... You would hopefully have URL routing so you would simply link to /ajax/update. This is the ideal solution.

Within such a method you would want to echo your results as opposed to trying to display a template/layout/view combination. This may mean you need to tweak your CMS to allow for such a thing but hopefully not.

Your method Update should more than likely have access to the globals $_POST and $_GET so that you may retrieve the data submitted from the AJAX request.

cballou
My current url scheme is /page/action/query. So if I was doing a search on a page it would look like /books/search/plato. The controller for class "Books" is called where it calls the method "search" and passes "plato" to the search method. So where I'm at now is I have a /playground page. Inside the class I have the method "gradient" which I want to apply to some user input text area. So I want to send the form through $_GET and and call "gradient" which isn't in the "handle.php". I could put it in there, but then the controller would be useless to me.
NeoTubNinja
A: 

You should have some way to determine which command you are calling, so the controller can determine where you need to go.

For example, if you pass in a form # and command, your controller may pass this off to the controller for that specific form, who can then determine how to do the operation you need.

I think it is risky to have all this logic in one controller, as it will mean a very long switch statement, at some point.

You may want to explain more about how your controller handles input from the javascript (client), to determine routing, as, currently, it is just guesswork as to how to help you.

James Black
Right now I have the controller get its commands from the url in the from of /page/action/query. I haven't used # for anything besides html anchors like 4 years ago, but it sounds like a good idea. As of right now I don't have a controller handling javascript which is my problem. I make the Ajax call to handle which can either have the logic in it, or autoload the controllers I need by having a controller per form(because I will eventually have a lot of forms). Neither of which appealed to me. If I can pass it off with the #, great.
NeoTubNinja
Using the href is a good choice, as you can then tell your controller which form, and what command. action="controller.php/formname/commandname". Your controller will then get the parts that is needs, splitting up the url by '/' perhaps, to determine where to send the request.
James Black
A: 

You're saying;

As of right now I don't have a controller handling javascript which is my problem

As I understand your controller doesn't need a controller handling an ajax request. You make the ajax request as you would a normal request. So if your controller knows what to do with a normal request its already handling javascript.

Again if I understand correctly for the books example you should do something like that;

$.get("/books/search/plato",  function(data){
    alert("Data Loaded: " + data);
});

And for the /playground and gradient thing this should work; You can invoke this from handle.php and it will request the playground controller and return the data;

$.get("/playground/gradient",  function(data){ //data is what is returned from playground file
    alert("Data Loaded: " + data);
});

Hope this helps

Sinan