我总是遇到发送邮件表单的问题,我认为这是因为有很多不同的代码需要,这就是原因;
这是邮件拼图,从html代码开始然后脚本结束然后php
HTM CODE:
<div class="col-2 animation_fade_in_block">
<div class="form-style" id="contact_form_2">
<div id="contact_body">
<label>
<input type="text" name="name" id="name" required class="input-field" placeholder="Name *" />
</label>
<label>
<input type="email" name="email" required class="input-field" placeholder="E-mail *" /> </label>
<label>
<input type="text" name="phone" maxlength="19" required placeholder="Phone *" class="tel-number-field long" /> </label>
<label for="field5">
<textarea name="message" id="message" class="textarea-field" required placeholder="Message *"></textarea>
</label>
<label>
<input type="submit" id="submit_btn_2" class="btn" value="Send message " /> </label>
</div>
<div id="contact_results"></div>
</div>
</div>
SCRIPT
$("#submit_btn_2").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_2 input[required=true], #contact_form_2 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 = {
'user_name': $('input[name=name]').val(),
'user_email': $('input[name=email]').val(),
'phone_number': $('input[name=phone]').val(),
'subject': $('select[name=subject]').val(),
'msg': $('textarea[name=message]').val()
};
//Ajax post data to server
$.post('../php/contact.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_2 input[required=true], #contact_form_2 textarea[required=true]").val('');
$("#contact_form_2 #contact_body").fadeOut("fast"); //hide form after success
}
$("#contact_form_2 #contact_results").hide().html(output).slideDown();
}, 'json');
}
});
//reset previously set border colors and hide all message on .keyup()
$("#contact_form_2 input[required=true], #contact_form_2 textarea[required=true]").keyup(function() {
$(this).css('border-color', '');
$("#contact_results").fadeOut("fast");
});
$("#submit_btn_2").click(function(e) {
e.preventDefault();
//Rest of your code
});
PHP代码
<?php
if($_POST)
{
$to_email = "xxxxxxxx@gmail.com"; //Recipient email, Replace with own email here
//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().
$user_name = filter_var($_POST["user_name"], FILTER_SANITIZE_STRING);
$user_email = filter_var($_POST["user_email"], FILTER_SANITIZE_EMAIL);
$phone_number = filter_var($_POST["phone_number"], FILTER_SANITIZE_NUMBER_INT);
$message = filter_var($_POST["msg"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($user_name)<3){ // If length is less than 3 it will output JSON error.
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation
$output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
die($output);
}
if(!filter_var($phone_number, FILTER_SANITIZE_NUMBER_FLOAT)){ //check for valid numbers in phone number field
$output = json_encode(array('type'=>'error', 'text' => 'Enter only digits in phone number'));
die($output);
}
if(strlen($message)<3){ //check emtpy message
$output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
die($output);
}
//email body
$message_body = "
Name:".$user_name."\r\n
Email: ".$user_email."\r\n
Phone Number: ". $phone_number."\r\n
Message:".$message
;
//proceed with PHP email.
$headers = 'From: '.$user_name.'' . "\r\n" .
'Reply-To: '.$user_email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$send_mail = mail($to_email, "Message from site", $message_body, $headers);
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' => '
<div class="popup_icon"><i class="fa fa-check "></i></div>
<div class="popup_title">Спасибо за контакт со STYX!</div>
<div class="popup_subtitle">Мы ответим очень скоро.</div>
'));
die($output);
}
}
?>
问题是什么;问题是我有&#34;名字太短或空了!&#34;即使我写了很长的名字也会出错。有没有人能帮到我?
提前致谢
答案 0 :(得分:0)
你可能需要两件事来解决问题。
user_name
POST或无法从选择器中找到值。$_POST['user_name']
并删除大量字符串。故障排除:
1。
使用Chrome或Firefox,打开开发者控制台或开发人员工具(在Chrome中,可在Options > More Tools > Developer Tools
单击“控制台”选项卡。
接下来两个>
符号输入$('input[name=name]').val()
如果它返回undefined
- 这意味着浏览器找不到此选择器。
潜在修复,请改用:
$("input[name='name']").val()
因为name
属性也是name
,jQuery可能很难解析它。 *猜测。
2。
检查您的PHP是否获得了正确的$_POST
数据
我喜欢快速而又肮脏
echo json_encode(array("type"=>"error", "text" => print_r($_POST,true));
exit();
在剧本的最开始。
接下来尝试评论:
//Sanitize input data using PHP filter_var().
// > $user_name = filter_var($_POST["user_name"], FILTER_SANITIZE_STRING);
如果这样做,那么你知道Filter var正在杀死这个变量。
答案 1 :(得分:0)
我已经尝试过所有想法,首先要感谢所有想法。 问题是有两个联系表格,两个&#34;名称和#34;参数混合在一起。我添加#contact_form和#contact_form_2以便修复..
'user_name' : $('#contact_form input[name=name]').val(),
'user_email' : $('#contact_form input[name=email]').val(),
'phone_number' : $('#contact_form input[name=phone]').val(),
'subject' : $('#contact_form select[name=subject]').val(),
'msg' : $('#contact_form textarea[name=message]').val()
'user_name' : $('#contact_form_2 input[name=name]').val(),
'user_email' : $('#contact_form_2 input[name=email]').val(),
'phone_number' : $('#contact_form_2 input[name=phone]').val(),
'subject' : $('#contact_form_2 select[name=subject]').val(),
'msg' : $('#contact_form_2 textarea[name=message]').val()