$database = new DBFunctions(); //This is the array where the JSON data will be returned $data = array("mode" => $mode, "success" => 0, "error" => 0); if ($mode == 'register') { //Extract all data that we need in order to register a user $name = $_POST['name']; $year = $_POST['year']; $class = $_POST['class']; if ($database->isUserRegistered($name)) { $data["success"] = 0; $data["error"] = 2; $data["message"] = "This user is already registered!"; echo json_encode($data); } else { //We can insert the user in the database $tmp_user = $database->saveUser($name, $year, $class); //This means the user was saved in the database with success if ($tmp_user != false) { $data["success"] = 1; $data["error"] = 0; $data["user"]["name"] = $tmp_user["name"]; $data["user"]["year"] = $tmp_user["year"]; $data["user"]["class"] = $tmp_user["class"]; $data["message"] = "The user was saved in the database!"; echo json_encode($data); } else { //Failed to store a user in the database $data["success"] = 0; $data["error"] = 1; $data["message"] = "Something went wrong while registering the user!"; echo json_encode($data);