どのチェック ボックスが選択されているかによって、2 つの異なるメール アドレスに送信されるメール フォームを作成しました。しかし、それを機能させるのに本当に苦労しています。私がやろうとしていることをお見せしましょう。
フォームが実際にうまく機能することを念頭に置いて、チェックボックスを介してメールを送信するだけで失敗します。
助けてください!
これはフォーム セクションです。
<div id="respond">
<?php echo $response; ?>
<form id="commentForm" method="post" action="#contact2">
<div>
<small>Name:</small>
<input type="text" name="message_name" placeholder="Enter you name..." value="<?php echo $_POST['message_name']; ?>">
<small>Department:</small>
<!-- EDIT || replaced type=checkbox with type=radio -->
<input type="radio" name="sales" value="sales@h-s-c.co.uk" checked/>
<input type="radio"name="lettings" value="lettings@h-s-c.co.uk" />
<small>Email:</small>
<input type="text" name="message_email" placeholder="somebody@email.co.uk" value="<?php echo $_POST['message_email']; ?>">
<small>Phone:</small>
<input class="" type="tel" name="phone_number" placeholder="07912 208 936" value="<?php echo $_POST['phone_number']; ?>">
</div>
<div style="float:right;">
<small>Message:</small>
<textarea type="text" name="message_text" placeholder="I would like further information on..."><?php echo $_POST['message_text']; ?></textarea>
</div>
<small>Human Verification:</small><br>
<input type="text" style="width: 60px;" name="message_human" placeholder="2"> + 3 = 5</label>
<input type="hidden" name="submitted" value="1">
<input class="" type="submit" value="submit"/>
</form>
</div>
次に、フォームを送信する php は次のようになります。
<?php
//Include email and to name
$admin_email = $_POST[$email_address];//EDIT || changed to new value $email_address
$block = "Test";
//Remove this section
//foreach($_POST['check'] as $value) {
// $checkbox .= "$value\n";
//}
?>
<?php
//response generation function
$response = "";
//function to generate response
function generate_response($type, $message){
global $response;
if($type == "success") $response = "<div class='success'>{$message}</div>";
else $response = "<div class='error'>{$message}</div>";
}
//response messages
$not_human = "Human verification incorrect.";
$missing_content = "Please supply all information.";
$email_invalid = "Email Address Invalid.";
$message_unsent = "Message was not sent. Try Again.";
$message_sent = "Thanks! Your message has been sent.";
//user posted variables
$name = $_POST['message_name'];
$email = $_POST['message_email'];
$phone = $_POST['phone_number'];
$message = $_POST['message_text'];
$human = $_POST['message_human'];
//php mailer variables
$to = $admin_email;
$subject = "Someone sent a message from {$block}";
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
$tel = 'Contact no: ' . $phone;
if(!$human == 0){
if($human != 2) generate_response("error", $not_human); //not human!
else {
//validate email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
generate_response("error", $email_invalid);
else //email is valid
{
//validate presence of name and message
if(empty($name) || empty($message)){
generate_response("error", $missing_content);
}
else //ready to go!
{
$sent = mail($to, $subject, $message . $tel , $headers);
if($sent) generate_response("success", $message_sent); //message sent!
else generate_response("error", $message_unsent); //message wasn't sent
}
}
}
}
else if ($_POST['submitted']) generate_response("error", $missing_content);
?>