Welcome Guest, Not a member yet? Register   Sign In
How to post for ajax (js) disabled Browsers when you use another controller?
#1

[eluser]breaddes[/eluser]
I have a controller that manages everything about comments. That means if you comment a review, story or article an ajax request goes out to the comment controller. If it was successful you get a little html-fragment back that says something like: "Your comment was saved successfully." That's simple and it works.

Now that sounds nice with ajax but we have to care about users that don't have javascript enabled. If they dispatch the comment form they will be directed to the comment controller which as a matter of course only outputs a little html fragment. They get lost of the actual site context.

The only solution I can think of is putting the comment controller code in every controller that needs a functionality to comment. Then everything would be posted back to the same site and the output would be fine. That isn't a nice way to do it because you have multiple lines of the same code.

Does anyone have an advice for that?
#2

[eluser]bl00dshooter[/eluser]
You could have 2 outputs, one just the fragment and the other the whole page, something like this:

Code:
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
//ouput just fragment
}
else {
//output everything
}

There are others way of doin it, but that's the simplest thing I could come up with.
#3

[eluser]breaddes[/eluser]
The comment controller doesn't know what the whole page looks like. At the moment it just outputs the html-Fragment which is then recieved as response in ajax. It's doesn't matter where the ajax is placed. It could be anywhere.

The comment controller is just an abstract collection of comment methods. It's independent from the actual site controller.
#4

[eluser]InsiteFX[/eluser]
MY_Controller

InsiteFX
#5

[eluser]breaddes[/eluser]
As far as I know MY_Controller extends a Controller to make additional custom methods available. That sounds like an idea but what if it's not only the comment functionality that a controller needs to be extended? Do I write all additional methods in MY_Controller?
#6

[eluser]InsiteFX[/eluser]
If all your controllers are going to use the same fuctions yes!

If not then code them in the controllers that need them.

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB