NYCPHP Meetup

NYPHP.org

[nycphp-talk] Ajax 101: what to return from a POST

David Mintz vtbludgeon at gmail.com
Thu May 3 16:35:05 EDT 2007


Thank you! This is consistent with what I'm trying to do, I just wanted to
confirm that my approach is basically sane. Your example gives me something
to study and steal from.

On 5/3/07, Mark Armendariz <lists at enobrev.com> wrote:
>
> >>> From: talk-bounces at lists.nyphp.org [mailto:
> talk-bounces at lists.nyphp.org]
> On Behalf Of David Mintz
> >>>    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.
>
> I like the previously mentioned ideas of returning an error code in the
> header.  I currently just return an error array in the returned data.  I
> definitely agree that sending back an entire form that already exists is
> too
> much.
>
> I do something very similar to your method.  If your fields have id's
> based
> on their names you can manipulate them pretty easily to show the error.
> Here's something similar to how I do it...
>
> /*
> * ids are field_<field_name> so an email field would have an id of
> field_email
> *** oReturnedData would be a JSON object from the server
> */
>
> // example var
> var oReturnedData = {
>         errors : [
>                         {field: 'email', message: 'we do not accept
> hotmail
> accounts'},
>                         {field: 'age',   message: 'you must be over 21 to
> register'},                                     {field: 'phone', message:
> 'we both know this is not a phone number'}
>                    ],
>         otherstuff : []
>     }
>
> if (oReturnedData.errors !== undefined)
> &&  oReturnedData.errors.length) {
>     $A(oReturnedData.errors).each(
>         function(oError) {
>             if ($('field_' + oError.field) !== null) {
>                 var sField = '<p class="error">' + oError.message + '</p>'
>                 new Insertion.After($(oError.field), sField);
>             }
>         }.bind(this)
>     );
> }
>
> I usually manipulate the field labels, but I figured this shows the
> general
> idea best and simplest.
>
>

-- 
David Mintz
http://davidmintz.org/

Just a spoonful of sugar helps the medicine go down
In the most delightful way.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20070503/afed5bbb/attachment.html>


More information about the talk mailing list