How can radio buttons in a PHP contact form be used to determine the subject of the email being sent?
To determine the subject of the email being sent from a PHP contact form based on radio buttons, you can assign different values to each radio button representing different subjects. When the form is submitted, you can use an if-else statement to check which radio button is selected and set the email subject accordingly.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$subject = "";
// Check which radio button is selected
if ($_POST['subject'] == 'general') {
$subject = 'General Inquiry';
} elseif ($_POST['subject'] == 'support') {
$subject = 'Support Request';
} elseif ($_POST['subject'] == 'feedback') {
$subject = 'Feedback';
}
// Use $subject in the email headers
$to = 'recipient@example.com';
$headers = "From: " . $_POST['email'] . "\r\n";
$headers .= "Reply-To: " . $_POST['email'] . "\r\n";
$headers .= "Subject: " . $subject . "\r\n";
// Send the email
mail($to, $subject, $_POST['message'], $headers);
}
?>