Radio INPUT element Multiple submit buttons in PHP

 Radio INPUT element Multiple submit buttons in PHP

If you have multiple submit buttons within a form, and you want to determine which button was clicked in your PHP script, you can achieve this by checking the presence of the button's name in the `$_POST` data.

 example:  HTML Form with Multiple Submit Buttons and Radio Buttons:

 <!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

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

    <title>Multiple Submit Buttons 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>

 

        <br>

 

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

        <input type="submit" name="resetButton" value="Reset">

    </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.";

    }

 

    // Check which submit button was clicked

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

        echo "<br>Submit button clicked!";

    } elseif (isset($_POST['resetButton'])) {

        echo "<br>Reset button clicked!";

    }

} 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 how to handle multiple submit buttons along with radio buttons in PHP.

-------------------------------------------------------------------------------------------------------

Post a Comment

0 Comments