I just want to know if is ok to use echo
And return
instead one function
The below code is one of my function
if(!function_exists("un_block_ad_ajax")) {
function un_block_ad_ajax() {
global $db;
$is_admin = is_admin();
$check_user = true;
$request = $_POST["data"]["request"] ?? "";
if(!in_array($request,["block","publish"])) {
return false;
}
$ad_id = $_POST["data"]["ad_id"] ?? "";
$ad_id = (int) $ad_id;
if(empty($ad_id)) {
return false;
}
if($is_admin === true) {
$check_user = false;
}
if($check_user === true) {
if(is_login() === false) {
return false;
}
$get_ad_info = $db->select("SELECT ad_publisher FROM ads WHERE id = ? LIMIT 1",[$ad_id],["%i"]);
$get_ad_info = $get_ad_info[0];
if($get_ad_info["ad_publisher"] != is_login()->id) {
// User-undesired activity
return false;
}
}
$update_status = $db->update("ads",["ad_status" => $request],["%s"],["id" => $ad_id],["%i"]);
if($update_status) {
echo json_encode(["success" => true]);
}
return true;
}
}
I call function from one file
$method = $_POST["method"] ?? : "";
if(!empty($method)) {
$allowed_methods = array(
"signup_ajax",
"signin_ajax",
"add_ad_ajax",
"upload_ad_image",
"send_message",
"get_message",
"get_cnv",
"del_add_to_fav",
"delete_ajax",
"un_block_ad_ajax",
);
if(in_array($method,$allowed_methods)) {
if(call_user_func($method) === false) {
exit(http_response_code("400"));
}
}else{
exit(http_response_code("404"));
}
}else{
exit(http_response_code("404"));
}
So as you see in code I need to use return and echo
Technically, you can do as you may need, however, I advise you maintain a standard, as in:
Let's say you want to return true to an operation now, but tomorrow you need true and a wholesome set of data of, for example, what was updated.
You may want to build a generic response for your application, where you can use the same function (or logic) to read every response you may eventually get.
A response containing of:
{
'success' => true,
'message' => [...],
'visibleToHumans' => true,
'content' => [...]
}
You can have an UI that handles the response of your request, that can read the content based off success.
Extra: I'd like to advise to look up PDOStatements, to secure your queries.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.