In terms of the ajax call, the fact that the php script has returned
data (empty or otherwise) is a success, and you can't alter that. The
ajax call does not itself care what is in the data, simply that the
communication with the backend script was successfully completed.
You therefore need to structure your returned data appropriately for
backend error conditions, and handle it within the success function of
the ajax call. This is simple with, for example, JSON data, but
possibly less so with HTML.
With JSON data, for example...
PHP might return:
{ ok:false
, numErrors:3
, error:[ 'Missing name field'
, 'Missing address field'
, 'Bad email address'
]
}
...or...
{ ok:true
, numFields:3
, field:{ name : 'Joe Bloggs'
, address : 'Here, There, Everywhere'
, email : '[EMAIL PROTECTED]'
}
}
so your success function could test data.ok and handle the rest of the
data as appropriate...
success : function(data){
if(data.ok){
//... data good, so process as success
}else{
//...data bad, so process as failure
}
}
For HTML data being returned, you could, for example....
PHP might return:
<div id='returnData'>
<div id='returnErrors'>
<div class='form_error'>Missing name field</div>
<div class='form_error'>Missing address field</div>
<div class='form_error'>Bad email address</div>
</div>
</div>
...or...
<div id='returnData'>
<div id='returnSuccess'>
<div class='form_success'>Data added successfully!</div>
</div>
</div>
so your success handler might create the HTML, look for #returnSuccess
and handle as appropriate....
success : function(data){
var $data = $(data)
, ok = $data.find('#returnSuccess');
if(ok.length){
//... data good, so $data.appendTo('#goodForm');
}else{
//... data bad, so $data.appendTo('#badForm');
}
}
The return data structure, the type of data, are down to you, but
basically you need to handle it in the success callback.
HTH
On Nov 15, 7:53 am, 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