You could force your PHP to respond with an error header, but I
personally don't think that is a very good practice.

What I've done in the past is have the PHP script return a JSON
response that would look something like:

In the case of no validation errors:
{'valid':'true'}

In the case of validation errors:
{'valid':'false', 'error_msg': 'your validation errors here'}

Then you'd have:

$.ajax({
        type: "POST",
        url: "script.php",
        dataType: 'json',
        data: {
                ...........
        },
        success:
                function(t) {
                    if (t.valid) {
                        .... show the result of successful ajax call
                    }
                    else {
                        .... show the result of t.error_msg
                    }
                },
        error:
                function() {
                .... show the result of a failure : server timeout
etc.
                }

});
}

Just an idea ;)

-Eric

On Nov 14, 11:53 pm, Irfan <[EMAIL PROTECTED]> wrote:
> Sorry if this has been posted before but I searched the group and
> could not find a clue.
> I have a problem which I believe has a simple solution but could not
> come up with a good way.
>
> While using Jquery with PHP, I want error checking to be implemented
> on the server side - with PHP. As it's much safer and also easier for
> me. All of the articles/tutorials I've seen about Jquery uses client
> side Javascript.
>
> Whilst I have no  problems doing this with PHP , if the server side
> validation fails [ex: missing a required field or an out of range
> value in a form etc] I would like the results of this to be printed as
> a result of errror: section.
>
> A typical ajax request is :
>
> $.ajax({
>         type: "POST",
>         url: "script.php",
>         data: {
>                 ...........
>         },
>         success:
>                 function(t) {
>                 .... show the result of successful ajax call
>                 },
>         error:
>                 function() {
>                 .... show the result of a failure : server timeout etc.
>                 }
>
> });
> }
>
> If the validation fails I would like to output the error messages as a
> result of the error: section because on the success part I usually
> hide/fadeout the form elements. But as the PHP script has been
> completed successfully output of the php falls to success section
>
> I try to overcome this problem by attaching another event to the
> output of script, ie: document.ready.function( .....
> but this is really cumbersome.
>
> I would highly appreciate other people's solutions, ideas on this
> subejct
>
> Irfan

Reply via email to