Send PHP Form to Different Emails Based on Radio B

2019-08-31 20:19发布

问题:

I've seen a number of questions similar to this one, but since I'm new to PHP I'm having trouble putting the function in the right place. I've got something cobbled together in PHP and HTML but I seem to be missing something around the $radio area. Does anyone have any suggestions?

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$radio = isset($_POST['radio']) ? $_POST['radio'] : 'default';
switch ($radio) {
case 'Eval CH':
$to = 'blah@blah.com';
    break;
case 'Eval ELL':
$to = 'sigh@sigh.com';
    break;
}
$message = $_POST['message'];
$from = 'From: WAC Site';  
$subject = 'WAC Contact Message';
$human = $_POST['human'];

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

if ($_POST['submit'] && $human == '4') {                 
    if (mail ($to, $subject, $body, $from)) { 
    echo '<p>Your message has been sent!</p>';
} else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
} 
} else if ($_POST['submit'] && $human != '4') {
echo '<p>Either you are a robot, or you are very, very bad at math.</p>';
}
?>

Here's my HTML:

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

<label>Name</label><br>
<input name="name" placeholder="Type Here"><br>
<br>        
<label>Email</label><br>
<input name="email" type="email" placeholder="Type Here"><br>
<br>
<input type="radio" name="Eval CH" value="to">Email Blah<br>
<input type="radio" name="Eval ELL" value="to">Email Sigh<br>
<label>Message</label><br>
<textarea name="message" placeholder="Type Here"></textarea><br>
<br>
<label>*What is 2+2? (Anti-spam)</label>
<input name="human" placeholder="Type Here"><br />         
<input id="submit" name="submit" type="submit" value="Submit"><br>

</form>

回答1:

The problem is that $_POST['radio'] doesn't exist the way you have your form setup. Only things with a name= are passed to the $_POST superglobal.

Change the condition in your PHP handler to this:

$radio = isset($_POST['Eval CH']) ? $_POST['Eval CH'] : $_POST['Eval ELL'];

But, you really have 3 conditions here:

$_POST['Eval CH']
$_POST['Eval ELL']
None selcted

So really your shorthand doesn't work here because you need

if
elseif
else

So you really need more advanced handling, like so:

if (isset($_POST['Eval CH'])) {
   $to = 'blah@blah.com';
}
elseif (isset($_POST['Eval ELL'])) {
   $to = 'sigh@sigh.com';
}
else {
   $to = 'default@default.com';
}

Hope this helps.



回答2:

Your radio buttons should have a common name and proper value. They should look like this:

<input type="radio" name="contact_option" value="Eval CH">Email Blah<br>
<input type="radio" name="contact_option" value="Eval ELL">Email Sigh<br>

Then, in your post you can replace your line with this:

$radio = isset($_POST['contact_option']) ? $_POST['contact_option'] : 'default';

Also, if ($_POST['submit']) { } should go around that whole section of code when you post, not just checking if you're human.



回答3:

try to reverse name and value in your radio button

<input type="radio" name="to" value="Eval CH">Email Blah<br>
<input type="radio" name="to" value="Eval ELL">Email Sigh<br>