Last active
January 25, 2022 17:47
-
-
Save joeworkman/663ad16459b925b427b6f9f431344058 to your computer and use it in GitHub Desktop.
This is a sample of a PHP script that could be used with the Custom AJAX request for Foundation 6 Stacks at foundationstacks.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('display_errors', '0'); | |
function returnError($msg) | |
{ | |
header('HTTP/1.1 500 Internal Server Error'); | |
header('Content-Type: application/json; charset=UTF-8'); | |
die(json_encode(array('message' => trim(strip_tags($msg)), 'code' => 500, 'post' => $_POST))); | |
} | |
function returnSuccess($msg) | |
{ | |
header('Content-Type: application/json'); | |
echo json_encode(array('message' => trim(strip_tags($msg)), 'code' => 200, 'post' => $_POST)); | |
exit(); | |
} | |
register_shutdown_function(function () { | |
$lastError = error_get_last(); | |
if (!empty($lastError) && $lastError['type'] == E_ERROR) { | |
returnError($lastError["message"]); | |
} | |
}); | |
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || count($_POST) === 0) { | |
// Only allow POST requests and make sure that there is POST data | |
returnError("Error: Invalid request"); | |
} | |
// Do something with the form data | |
foreach ($_POST as $field => $value) { | |
// ... Something cool ... | |
} | |
if ($someError) { | |
returnError("Houston, we have a problem"); | |
} | |
returnSuccess("One small step for man. One giant leap for mankind."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment