[jQuery] Re: Delibarately create ajax errors from PHP script
I know that you can use JSON or XML for the Ajax response section but somehow I had not thought of using JSON for this error checking part. Thanks for the answers. Have a nice day On Nov 15, 4:13 pm, Eric Martin <[EMAIL PROTECTED]> wrote: > 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
[jQuery] Re: Delibarately create ajax errors from PHP script
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
[jQuery] Re: Delibarately create ajax errors from PHP script
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: Missing name field Missing address field Bad email address ...or... Data added successfully! 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