根据单选按钮将PHP表单发送到不同的电子邮件 [英] Send PHP Form to Different Emails Based on Radio Buttons
问题描述
我已经看到了许多类似于这个问题的问题,但是因为我是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';
休息;
案例'Eval ELL':
$ to ='sigh@sigh.com';
休息;
}
$ message = $ _POST ['message'];
$从='From:WAC Site';
$ subject ='WAC联系信息';
$ human = $ _POST ['human'];
$ body =From:$ name\\\
电子邮件:$ email \\\
消息:\ $ $ message;
if($ _POST ['submit']&& $ human =='4'){
if(mail($ to,$ subject,$ body,$ from) ){
echo'< p>您的讯息已发送!< / p>';
} else {
echo'< p>出错了,请返回并重试!< / p>';
} else if($ _POST ['submit']&& $ $ human!='4'){
echo'< p>要么你是机器人,要么你在数学上非常非常糟糕。< / p>';
}
?>
这是我的HTML:
< form method =postaction =contact_options.php>
< label>名称< / label>< br>
< input name =nameplaceholder =Type Here>< br>
< br>
< label>电子邮件< / label>< br>
< input name =emailtype =emailplaceholder =输入此处>< br>
< br>
< input type =radioname =Eval CHvalue =to> Email Blah< br>
< input type =radioname =Eval ELLvalue =to> Email叹息< br>
< label>讯息< / label>< br>
< br>
< label> *什么是2 + 2? (反垃圾邮件)LT /标签>
< input name =humanplaceholder =Type Here>< br />
< input id =submitname =submittype =submitvalue =Submit>< br>
< / form>
问题在于 $ _POST ['radio']
不存在你表单设置的方式。只有 name =
的东西才会被传递给 $ _ POST
superglobal。
将PHP处理程序中的条件更改为:
$ b $ radio = isset($ _ POST ['Eval CH']) ? $ _POST ['Eval CH']:$ _POST ['Eval ELL'];
但是,您真的有三个条件: / p>
$ _ POST ['Eval CH']
$ _POST ['Eval ELL']
无selcted
所以你的速记在这里不起作用,因为你需要
if
elseif
else
$ b $所以你真的需要更高级的处理,如下所示:
if(isset($ _ POST ['Eval CH' ])){
$ to ='blah@blah.com';
}
elseif(isset($ _ POST ['Eval ELL'])){
$ to ='sigh@sigh.com';
}
else {
$ to ='default@default.com';
}
希望这有助于您。
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>
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.
这篇关于根据单选按钮将PHP表单发送到不同的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!