Last active
April 24, 2021 16:39
-
-
Save cosmomathieu/c0f97984667e2eaa65a07bc416ccda5a to your computer and use it in GitHub Desktop.
A PHP json object template for dealing with php and ajax form submissions
This file contains 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 | |
/** | |
* A PHP json object template for dealing with php and ajax form submissions | |
* | |
* Provides a framework (for lack of better words) for dealing with ajax | |
* form submissions. Provides an example of how the php and JavaScript code | |
* should be formatted. | |
* | |
* @author Cosmo Mathieu <[email protected]> | |
*/ | |
class JsonResponse | |
{ | |
/** | |
* Will always be true or false. If you want to send back a response | |
* message, set the $message variable. | |
* @var Boolean | |
*/ | |
public $status = false; | |
/** | |
* | |
* @var Array | |
*/ | |
public $messages = 'The query returned an empty set.'; | |
/** | |
* Holds data returned by a service or passthrough data. | |
* @var mixed | |
*/ | |
public $result = 0; | |
/** | |
* | |
* @var String - Event name triggered in JavaScript when service call successful. | |
*/ | |
public $onSuccessEvent; | |
/** | |
* | |
* @var String - Event name triggered in JavaScript when service call fails. | |
*/ | |
public $onErrorEvent; | |
/** | |
* Holds the errors generated in the Service object | |
* @var Array | |
*/ | |
private $errors; | |
} | |
if(is_ajax()) | |
{ | |
$ctrl = new LoginController(); // This is an example of how this could be used with a custom object | |
$result = new ServiceResult(); | |
$result->status = $ctrl->login($_POST['username'], $_POST['password']); // This would return true or false | |
$result->message = $ctrl->errors; | |
$result->result = 0; | |
$result->onSuccessEvent = 'refreshPage'; | |
$result->onErrorEvent = 'userLoginFailed'; | |
header("Content-Type: application/json"); | |
echo json_encode($result); | |
// OR | |
// echo json_encode([ | |
// success => true, // Indicates the status of the call | |
// errors => 0, // Indicates whether there were errors | |
// messages => '', // The message that acompanies the success or error | |
// result => '', // The actual data to be returned to the ajax method | |
// ]); | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Json Form Handling</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<meta name="description" content="" /> | |
<meta name="keywords" content="" /> | |
<script> | |
$.ajax({ | |
type: "POST", | |
url: "/ajax/request.html", | |
data: {action: 'test'}, | |
dataType:'JSON', | |
success: function(response){ | |
// put on console what server sent back... | |
if(response.length !== ''){ | |
console.log(response.status, response.data); | |
$('js-response').html(response.message); | |
} else { | |
console.log(response.status, response.message); | |
$('js-response').html(response.message); | |
} | |
} | |
}); | |
</script> | |
</head> | |
<body> | |
<div class="js-response"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment