If there are validation errors, send back a json-encoded false in the
X-JSON header, and some error HTML to be injected into a DOM element
in the body.
If there are no errors, send back a json-encoded true in the X-JSON
header, and do whatever else you need in the responseText.
So your script does something like:
new Ajax.Request('/some/service/url',{ onSuccess : function
(transport,json){
if (json){
//everything is great.
}else{
//ruh-roh
$('ErrorTextDiv').update(transport.responseText);
}
});
On May 3, 2007, at 12:31 PM, David Mintz wrote:
Let's say you are displaying a form populated with data from a db
table for a user to edit, and you want to AJAXify(with Prototype).
Your backend script does the validation. Suppose they POST it and
validation fails, what do you do?
I have experimented with converting a PHP array of error messages
(fieldName => errorMessage, etc) into JSON and sending that back,
then doing DOM scripting to stick the error messages into some
DIVs. Kind of a lot of work, but it's efficient in the sense that
you only send data that the front end needs.
The other option is to redraw the whole form with the error
messages in the DIVs and send back that chunk of HTML. Less
efficient in that you are sending back a bunch of bytes that are
the same as what was there in the first place, but more efficient
in that you re-use more code, and do less client-side acrobatics,
than if you return a JSON data structure.
Am I being a byte-Nazi? Any thoughts?
--
David Mintz
http://davidmintz.org/
Just a spoonful of sugar helps the medicine go down
In the most delightful way.
_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk
NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com
Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php
_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk
NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com
Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php