联系表单不起作用(Php 端)
Contact form is not working (Php side)
我想为我的网站制作一个联系表格,我正在关注这篇博文,但使其更简单(需要姓名、地址、消息)。
我正在我的机器上的本地主机上托管文件以执行 php。我认为我在 php 代码中有错误,但无法找到它。
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(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 = {
'user_name' : $('input[name=name]').val(),
'user_email' : $('input[name=email]').val(),
'msg' : $('textarea[name=message]').val()
};
console.log(post_data)
//Ajax post data to server
$.post('php/contact_me.php', post_data, function(response){
console.log(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');
}
});
PHP 文件:
<?php
if($_POST)
{
$to_email = "test@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);
$message = filter_var($_POST["msg"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($user_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($user_email, FILTER_VALIDATE_EMAIL)){ //email validation
$output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
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 = $message."'r'n'r'n-".$user_name."'r'nEmail : ".$user_email ;
//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, $subject, $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!'));
die($output);
}else{
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .' Thank you for your email'));
die($output);
}
}
?>
您需要有一个邮件服务器才能实际发送邮件。 你可以找到一个像 http://www.postfix.org/或 http://www.pmail.com/。 一旦您实际进行了设置,您应该能够发送邮件。
相关文章:
- 传递给 PHP 表单时 JavaScript 动态变量上的未定义索引
- 带有条件字段的PHP表单
- PHP表单提交和重定向
- 如何防止丢失PHP表单中输入的输入字段
- 在动态加载的PHP表单上放置JavaScript事件
- 正在创建'谢谢你'使用colorbox弹出PHP表单
- 带有jquery验证问题的php表单
- 如何将我通过JavaScript计算的自定义金额作为金额发送到PHP表单
- 提交时将JavaScript变量转换为PHP表单
- 带有多个提交按钮的PHP表单
- php表单验证n在表单上显示消息3秒钟并淡出
- php表单发布时正在加载进度条
- 带有javascript按钮的PHP表单-不起作用
- PHP表单适用于索引,但不适用于联系我们页面
- Cake php表单助手.替换输入值
- 对php表单的修改
- 如何在刷新时显示PHP表单中的随机文本和图像
- 自动完成PHP表单选择字段中的用户姓氏
- php表单中的JQuery-重定向到成功页面
- PHP表单,MailFoction和Google reCaptcha验证问题