Skip to content
PHP Logo

How can PHP developers ensure that the correct email addresses are associated with each dropdown menu selection in a form?

To ensure that the correct email addresses are associated with each dropdown menu selection in a form, PHP developers can use an associative array where the keys are the dropdown menu options and the values are the corresponding email addresses. When the form is submitted, the selected dropdown option can be used to retrieve the correct email address from the array.

php
<?php

// Define an associative array with dropdown menu options and corresponding email addresses
$emailAddresses = array(
    "Option 1" => "email1@example.com",
    "Option 2" => "email2@example.com",
    "Option 3" => "email3@example.com"
);

// Retrieve the selected dropdown option from the form submission
$selectedOption = $_POST['dropdown'];

// Get the email address associated with the selected option
$email = $emailAddresses[$selectedOption];

// Use the $email variable to send an email or perform any other necessary action