Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a server error response using php?

As soon as the user clicks the delete button my jQuery script asks the server to delete the selected item.

Now I want my php script to send a success or an error response.

Is it possible to fire the error callback in case the item could not be deleted?

Thanks


my jQuery code:

$.ajax({
 type: "post",
 url: "myAjax.php" ,
 dataType: "text",
 data: {
  some:data
 }, 
 error: function(request,error) 
 {
         // tell the user why he couldn't delete the item
         // timeout , item not found ...
 },

 success: function ( response )
 {
      // tell the user that the item was deleted
         // hide the item
 }
);
like image 523
jantimon Avatar asked Oct 27 '09 16:10

jantimon


People also ask

How to send a response in PHP?

For PHP versions 4.0: In order to send the HTTP response code, we need to assemble the response code. To achieve this, use header() function. The header() function contains a special use-case which can detect a HTTP response line and replace that with a custom one.

How to return status code in PHP?

To return a status code in PHP, the simplest way is to use the http_response_code() function, along with the relevant HTTP status code parameter into your website, followed by the exit() command which stops any more output being set.

What is Http_response_code in PHP?

The http_response_code() function sets or returns the HTTP response status code.

How do you set a status code in HTTP response?

Methods to Set HTTP Status Code Sr.No. This method sets an arbitrary status code. The setStatus method takes an int (the status code) as an argument. If your response includes a special status code and a document, be sure to call setStatus before actually returning any of the content with the PrintWriter.


1 Answers

header("HTTP/1.0 404 Not Found");
header('HTTP', true, 500); // 500 internal server error
// etc etc.. 

Check out header() for more options. All the HTTP error codes are available for use.

Related question: How do you trigger the "error" callback in a jQuery AJAX call using ASP.NET MVC?

like image 67
Mike B Avatar answered Sep 30 '22 17:09

Mike B