我正在尝试创建一个联系表单,可以根据用户选择的操作将邮件发送到不同的电子邮件。
这是我的代码:
<?php
if($_POST)
{
if(isset($_POST['ort1'])) {
$to="a@mail.com";
}
else {
$to="b@mail.com";
}
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
$output = json_encode(array( //create JSON data
'type'=>'error',
'text' => 'Sorry Request must be Ajax POST'
));
die($output); //exit script outputting json data
}
//Sanitize input data using PHP filter_var().
$r_name = filter_var($_POST["r_name"], FILTER_SANITIZE_STRING);
$r_email = filter_var($_POST["r_email"], FILTER_SANITIZE_EMAIL);
$ort1 = filter_var($_POST["ort1"], FILTER_SANITIZE_STRING);
$ort2 = filter_var($_POST["ort2"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($r_name)<4){ // If length is less than 4 it will output JSON error.
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if(!filter_var($r_email, FILTER_VALIDATE_EMAIL)){ //email validation
$output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
die($output);
}
//email body
$message_body = $r_name."\r\nEmail : ".$r_email;
//proceed with PHP email.
$subject = 'From: The president' .
$headers = 'From: '.$r_name.'' . "\r\n" .
'Reply-To: '.$r_email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$send_mail = mail($to, $subject, $headers, $message_body);
if(!$send_mail)
{
//If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}else{
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$r_name .' Thank you for your email'));
die($output);
}
}
?>
Html:
<div class="form-style" id="contact_form">
<div class="form-style-heading">Please Contact Us</div>
<div id="contact_results"></div>
<div id="contact_body">
<label><span>Name <span class="required">*</span></span>
<input type="text" name="name" id="name" required="true" class="input-field"/>
</label>
<label><span>Email <span class="required">*</span></span>
<input type="email" name="email" required="true" class="input-field"/>
</label>
<label>
<span>Norr</span><input type="checkbox" name="ort1">
<span>Söder</span><input type="checkbox" name="ort2">
</label>
<label>
<span> </span><input type="submit" id="submit_btn" value="Submit" />
</label>
</div>
jQuery的:
$(document).ready(function() {
$("#submit_btn").click(function() {
var proceed = true;
//simple validation at client's end
//loop through each field and we simply change border color to red for invalid fields
$("#contact_form input[required=true], #contact_form textarea[required=true]").each(function(){
$(this).css('border-color','');
if(!$.trim($(this).val())){ //if this field is empty
$(this).css('border-color','red'); //change border color to red
proceed = false; //set do not proceed flag
}
//check invalid email
var email_reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if($(this).attr("type")=="email" && !email_reg.test($.trim($(this).val()))){
$(this).css('border-color','red'); //change border color to red
proceed = false; //set do not proceed flag
}
});
if(proceed) //everything looks good! proceed...
{
//get input field values data to be sent to server
post_data = {
'r_name' : $('input[name=name]').val(),
'r_email' : $('input[name=email]').val(),
'ort1' : $('input[name=ort1]').val(),
'ort2' : $('input[name=ort2]').val()
};
//Ajax post data to server
$.post('process.php', post_data, function(response){
if(response.type == 'error'){ //load json data from server and output message
output = '<div class="error">'+response.text+'</div>';
}else{
output = '<div class="success">'+response.text+'</div>';
//reset values in all input fields
$("#contact_form input[required=true], #contact_form textarea[required=true]").val('');
$("#contact_form #contact_body").slideUp(); //hide form after success
}
$("#contact_form #contact_results").hide().html(output).slideDown();
}, 'json');
}
});
//reset previously set border colors and hide all message on .keyup()
$("#contact_form input[required=true], #contact_form textarea[required=true]").keyup(function() {
$(this).css('border-color','');
$("#result").slideUp();
});
});
正如你所看到我在开始时做了一个if循环但不幸的是它总是发送到a@mail.com。我一直试图找到另一种解决方案,但我无法理解为什么这个解决方案不起作用。