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);
}
?>
Related Questions
- What are the potential pitfalls of using PHP scripts for importing data into MySQL tables?
- What are the potential pitfalls of having the View handle data retrieval from the database in PHP applications?
- How can JavaScript, jQuery, or AJAX be integrated with PHP to achieve dynamic form field generation?