我已经看到了许多与此类似的问题,但由于我是PHP的新手,因此无法将该功能放在正确的位置。我用PHP和HTML拼凑了一些东西,但我似乎错过了$radio
区域周围的东西。有没有人有任何建议?
<?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>';
}
?>
这是我的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>
答案 0 :(得分:0)
问题是$_POST['radio']
与表单设置的方式不存在。只有name=
的内容才会传递给$_POST
超全局。
将PHP处理程序中的条件更改为:
$radio = isset($_POST['Eval CH']) ? $_POST['Eval CH'] : $_POST['Eval ELL'];
但是,你真的有三个条件:
$_POST['Eval CH']
$_POST['Eval ELL']
None selcted
所以你的速记真的不适合,因为你需要
if
elseif
else
所以你真的需要更高级的处理,如下:
if (isset($_POST['Eval CH'])) {
$to = 'blah@blah.com';
}
elseif (isset($_POST['Eval ELL'])) {
$to = 'sigh@sigh.com';
}
else {
$to = 'default@default.com';
}
希望这有帮助。
答案 1 :(得分:-1)
您的单选按钮应具有通用名称和适当的值。他们应该是这样的:
<input type="radio" name="contact_option" value="Eval CH">Email Blah<br>
<input type="radio" name="contact_option" value="Eval ELL">Email Sigh<br>
然后,在您的帖子中,您可以用以下内容替换您的行:
$radio = isset($_POST['contact_option']) ? $_POST['contact_option'] : 'default';
此外,if ($_POST['submit']) { }
应该在您发布时绕过整段代码,而不仅仅是检查您是否是人。
答案 2 :(得分:-1)
尝试在单选按钮中反转名称和值
<input type="radio" name="to" value="Eval CH">Email Blah<br>
<input type="radio" name="to" value="Eval ELL">Email Sigh<br>