Untitled

 avatar
unknown
plain_text
a month ago
1.0 kB
3
Indexable
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    header('Content-Type: application/json'); // Set JSON response header

    // Validate and sanitize input
    $firstName = htmlspecialchars($_POST['firstName'] ?? '');
    $gender = htmlspecialchars($_POST['gender'] ?? '');

    // Capture radio selection
    $carBrand = isset($_POST['carBrand']) ? htmlspecialchars($_POST['carBrand']) : 'No car brand selected';

    // Capture checkboxes (if any selected)
    $preferredBrands = isset($_POST['preferredBrands']) ? $_POST['preferredBrands'] : [];
    $preferredBrandsList = !empty($preferredBrands) ? implode(', ', array_map('htmlspecialchars', $preferredBrands)) : 'None';

    // Response message
    $response = [
        "status" => "success",
        "message" => "Hello, $firstName! You have selected $gender.",
        "carMessage" => "Your favorite car brand: $carBrand.",
        "preferredCarsMessage" => "You also like: $preferredBrandsList."
    ];

    echo json_encode($response); // Convert array to JSON and send response
}
?>
Editor is loading...
Leave a Comment