What’s the best way to add dynamic content to a web page after a successfull xml http request. To break down a more concrete example:
- User fills in data in input field (e.g. a forum post)
- Data is asynchrously updated using the ajax technology
- The forum post is instantly displayed for the end user
Sites like Facebook or last.fm (when you post a shout, ie) send the processed markup in a direct object back to the javascript, instead of just the processed data. An example of this written in jQuery would be
$('#activeField').html(callback.data);
Another way to do this is to create the dom elements on the fly with javascript. I find this too clumsy as there’s no easy (?) and simple way of doing this today. At the same time, sending the processed markup directly from the server violates our application’s design principles (MVC), as having markup in a front controller is not preferred.
What’s the “best practices” way of doing this? Thanks!
I may be missiing the point, but could you not send markup from the server by rendering it within a view as you would normally, rather than having to have markup in your controller? Assuming your View mechanism has the ability to return rendered HTML rather than outputting it immediately, you could retrieve this and then add it to an array before calling
json_encode()and outputting it. You could potentially then use the same view code to render this piece of HTML regardless of whether it is being fetched as part of a full page or via an AJAX call.