0

suppose i have a php database connection file. Consider two cases if there is an connection error :

  1. If request is a normal request (not ajax) then i would simply echo a message and exit.
  2. If request is a ajax request then i have to use json encode to send status and msg to client side js.

How can i separate the logic for above code, or how is this normally handled by you guys.

Consider another case : If a login in session is expired :

  1. If request is a normal request (not ajax) then i would simply use header('location: login.php');
  2. If request is a ajax request then ? (should i need to send a status code to client side and then use window.location )
1
  • What about using POST method in ajax and checking it in php code? if ($_SERVER['REQUEST_METHOD'] === 'POST') Commented Jul 3, 2018 at 10:06

1 Answer 1

0

Please use Below Code:

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{    
  echo "Ajax Request";
}
else{
  echo "Not an Ajax Request";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.