我有一个非常基本的技能,但这不应该太难。 我有一个非常基本的HTML格式:
<div class="form-input">
<div class="form-title">LOCATION</div>
<select id="form-select">
<option>GENERAL ENQUIRY</option>
<option>SHOP 1</option>
<option>SHOP 2</option>
<option>SHOP 3</option>
<option>SHOP 4</option>
<option>SHOP 5</option>
<div class="down-box"><i class="typcn typcn-chevron-down"></i></div>
</select>
</div>
<div class="form-input">
<div class="form-title">NAME</div>
<input id="form-name" required type="text">
</div>
<div class="form-input">
<div class="form-title">EMAIL</div>
<input id="form-email" required type="text">
</div>
<div class="form-input">
<div class="form-title">TELEPHONE</div>
<input id="form-telephone" required type="text">
</div>
<div class="form-input">
<div class="form-title">MESSAGE</div>
<textarea id="form-msg" type="text"></textarea>
</div>
<div class="form-input">
<div class="form-title"> </div>
<button id="form-send">SEND</button>
</div>
</div><!--end of form holder-->
和一个非常基本的mail.php从表单发送电子邮件:
<?php
if ($_POST) {
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$text = $_POST['text'];
$option = $_POST['option'];
$headers = $option . "\r\n" . $name . "\r\n" . $email . "\r\n" . $telephone;
//send email
mail("email@email.com", "Mail Enquiry", $text, $headers);
}
?>
现在,我想修改此php,根据下拉菜单中的选择将电子邮件发送到不同的地址。我该如何实现这一目标? (例如,如果用户选择SHOP1电子邮件发送到shop1 @ email ...,SHOP2发送电子邮件到shop2 @ email ..等等..)
提前感谢大家!
答案 0 :(得分:2)
首先,您的所有输入,选择和textareas都没有name
属性,因此不会将值从他们发送到$_POST[x];
。
<强> HTML:强>
正如@Syscall在评论中所说,select
中的名称和值已丢失。这是一个它应该是什么样子的例子:
<select id="form-select" name="sendTo">
<option value="general-enquiry">GENERAL ENQUIRY</option>
<option value="shop-1">SHOP 1</option>
<option value="shop-2">SHOP 2</option>
[...]
</select>
我强烈建议不使用值中的电子邮件地址,否则可以快速处理并滥用其他内容。
<强> PHP:强>
在PHP中,您可以使用array
,if
或switch
将邮件发送到所需的电子邮件地址。
这里有一个开关的例子:
switch ($_POST['sendTo']) {
case 'shop-1':
$sendTo = 'shop-1@mail.com';
break;
case 'shop-2':
$sendTo = 'shop-2@mail.com';
break;
[...]
default:
$sendTo = 'general-enquiry@mail.com';
break;
}
mail($sendTo, 'Mail Enquiry', $text, $headers);
我希望这有助于:)
根据要求,一旦完成必要的代码。
<强> HTML:强>
<form method="post">
<input type="hidden" name="sendMail" value="1" />
<label for="sendTo">Location</label>
<select id="sendTo" name="sendTo">
<option value="general-enquiry">General Enquiry</option>
<option value="shop-1">Shop 1</option>
<option value="shop-2">Shop 2</option>
<option value="shop-3">Shop 3</option>
<option value="shop-4">Shop 4</option>
<option value="shop-5">Shop 5</option>
</select>
<label for="name">Name <em>(required)</em></label>
<input id="name" type="text" name="name" placeholder="Enter your full name ..." required />
<label for="email">Email <em>(required)</em></label>
<input id="email" type="text" name="email" placeholder="Enter your best email ..." required />
<label for="telephone">Telephone <em>(required)</em></label>
<input id="telephone" type="text" name="telephone" placeholder="Enter your phone number ..." required />
<label for="message">Message</label>
<textarea id="message" name="message" placeholder="If you have a message for us or one of our shops, please enter them here ..."></textarea>
<input type="submit">Send</input>
</form>
<强> PHP:强>
if ($_POST['sendMail']) {
if (empty($_POST['name'])) {
return 'Please enter your full name!';
}
if (empty($_POST['email'])) {
return 'Please enter your email!';
}
if (empty($_POST['telephone'])) {
return 'Please enter your phone number!';
}
$subject = 'Mail Enquiry';
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$message .= 'Name: ' . $name . "\r\n";
$message .= 'Email: ' . $email . "\r\n";
$message .= 'Telephone: ' . $telephone . "\r\n";
if ($_POST['message']) {
$message .= "\r\n";
$message .= 'Message: ' . "\r\n";
$message .= $_POST['message'];
}
switch ($_POST['sendTo']) {
case 'shop-1':
$sendTo = 'shop-1@yourdomain.com';
break;
case 'shop-2':
$sendTo = 'shop-2@yourdomain.com';
break;
case 'shop-3':
$sendTo = 'shop-3@yourdomain.com';
break;
case 'shop-4':
$sendTo = 'shop-4@yourdomain.com';
break;
case 'shop-5':
$sendTo = 'shop-5@yourdomain.com';
break;
default:
$sendTo = 'general-enquiry@yourdomain.com';
break;
}
$header .= 'To: ' . $sendTo . "\r\n";
$header .= 'From: ' . $name . ' <' . $email . '>' . "\r\n";
if ($sendTo && $subject && $message && $header) {
mail($sendTo, $subject, $message, $header);
}
}
答案 1 :(得分:1)
您需要为自己的输入设置name
,因此如果您想通过<input id="form-name" required type="text">
<input id="form-name" name="name" required type="text">
,$_POST['name']
会变为<select id="form-select" name="shop-select">
<option>GENERAL ENQUIRY</option>
<option value="shop1">SHOP 1</option>
<option value="shop2">SHOP 2</option>
<option value="shop3">SHOP 3</option>
<option value="shop4">SHOP 4</option>
<option value="shop5">SHOP 5</option>
<div class="down-box"><i class="typcn typcn-chevron-down"></i>
LI>
您需要选择值。
<?php
if ($_POST) {
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$text = $_POST['text'];
$option = $_POST['option'];
$shop = $_POST['shop-select']
$headers = $option . "\r\n" . $name . "\r\n" . $email . "\r\n" .
$telephone;
//send email
mail(getEmail($shop), "Mail Enquiry", $text, $headers);
}
function getEmail($shop)
{
//an array of shops name and email that you get from somewhere
// store it in database or as a constant somewhere ? or you can declare it here...
$shops = [
"shop1" => "someemail@email",
// etc
];
if (isset($shops[$shop]) && filter_var($shops[$shop], FILTER_VALIDATE_EMAIL)) {
return $shops[$shop];
}
return "email@email.com";
}
?>
和
{{1}}
下一步你想做一些输入清理工作,如果你的项目足够大,可能会使用一个框架,所以所有的东西都已经为你完成了......