<?php

require_once "config.php";

// Set header to specify content type as JSON
header('Content-Type: application/json');

// Get the HTTP method used
$method = $_SERVER['REQUEST_METHOD'];

// Perform actions based on the HTTP method
switch ($method) {
    case 'GET':
        // Get the value of the "cnohp" parameter from the query string
        $cnohp = isset($_GET["cnohp"]) ? $_GET["cnohp"] : '';
        
        // Check if cnohp is provided
        if (!empty($cnohp)) {
            // Prepare the SQL query to check for the cnohp
            $stmt = $conn->prepare("SELECT cnohp FROM tbmember WHERE cnohp = ?");
            $stmt->bind_param("s", $cnohp);
            $stmt->execute();
            $result = $stmt->get_result();
            
            // Fetch the result
            if ($result->num_rows > 0) {
                $row = $result->fetch_assoc();
                $response = array("exists" => true, "cnohp" => $row['cnohp'], "message" => "Nomor Telepon ditemukan.");
            } else {
                $response = array("exists" => false, "message" => "Nomor Telepon tidak ditemukan.");
            }
            
            // Close the statement
            $stmt->close();
        } else {
            $response = array("error" => "Tidak ada nomor telepon yang diberikan.");
        }

        // Output the response as JSON
        echo json_encode($response);
        break;
    
    default:
        echo json_encode(array("error" => "Metode permintaan tidak valid."));
        break;
}

// Close the database connection
mysqli_close($conn);
?>
