PHP processing multiple processing HTML INPUT radio buttons in one form

 PHP processing multiple processing HTML INPUT radio buttons in one form

  • When processing multiple HTML input radio buttons in a PHP form, you'll handle them in a similar way to checkboxes.
  • You group radio buttons by giving them the same `name` attribute and different `value` attributes.

  HTML Form with Multiple Radio Buttons:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Radio Button Processing Example</title>

</head>

<body>

    <form action="process_form.php" method="post">

        <input type="radio" name="gender" value="male" id="male">

        <label for="male">Male</label>

 

        <input type="radio" name="gender" value="female" id="female">

        <label for="female">Female</label>

 

        <input type="radio" name="gender" value="other" id="other">

        <label for="other">Other</label>

 

        <input type="submit" value="Submit">

    </form>

</body>

</html>


PHP Script to Process the Form:

Create a PHP script (e.g., `process_form.php`) to handle the form data:

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    // Check if the 'gender' field is set in the $_POST data

    if (isset($_POST['gender'])) {

        // Retrieve the selected gender

        $selectedGender = $_POST['gender'];

 

        // Display the selected gender

        echo "Selected Gender: " . $selectedGender;

    } else {

        // If no gender is selected

        echo "No gender selected.";

    }

}

else

{

    // If the form is not submitted, redirect to the form page

    header("Location: index.html");

    exit();

}

?>


  • Adjust the form action (`action="process_form.php"`) and the PHP script based on your project structure and requirements.
  • This example demonstrates the basic handling of multiple radio buttons submitted in PHP.
--------------------------------------------------------------------------------------------

Post a Comment

0 Comments