新 - 发现我有一个javascript验证并在我的网站上提交表单 - 添加了相关的js
希望有人可以帮忙!我不是一个编码器,而是将元素拼接在一起以实现我的目的。有一个Web表单,我正在使用PHP提交到我的电子邮件但它没有发生。电子邮件未发送且成功或失败文本未显示 - 请求解决方案。
HTML
<!-- CONTACT FORM -->
<div class="col-sm-7 col-md-9">
<!-- IMPORTANT: change the email address at the top of the php/mail.php file to the email address that you want this form to send to -->
<form class="form-style validate-form clearfix" action="php/mail.php" method="POST" role="form">
<!-- form left col -->
<div class="col-md-6">
<div class="form-group">
<input class="text-field form-control validate-field required" data-validation-type="string" id="name" placeholder="Name" name="name" type="text">
</div>
<div class="form-group">
<input class="text-field form-control validate-field required" data-validation-type="email" id="email" placeholder="Email" name="email" type="email">
</div>
<div class="form-group">
<input class="text-field form-control validate-field required" data-validation-type="phone" id="phone" placeholder="Telephone" name="phone" type="tel">
</div>
</div><!-- end: form left col -->
<!-- form right col -->
<div class="col-md-6">
<div class="form-group">
<textarea placeholder="Message..." class="form-control validate-field required" name="message"></textarea>
</div>
<div class="form-group">
<img src="images/loader-form.GIF" class="form-loader">
<button type="submit" name="submit" class="btn btn-sm btn-outline-inverse">Submit</button>
</div>
<div class="form-group form-general-error-container">
<?php echo $result; ?>
</div>
</div><!-- end: form right col -->
</form>
</div><!-- end: CONTACT FORM -->
JS 这只是脚本的验证和提交部分 - functions.js。我已经从HTML中删除了验证码元素,因为我不想留下它 - 这可能会也可能不会导致验证问题。
function validate_and_submit_forms(b){var a=(b!==undefined&&b.length>0)?b:$("form.validate-form");a.each(function(){var
c=$(this);c.find(".validate-field").each(function(){$(this).change(function(){$(this).siblings(".alert").fadeOut("fast",function()
{$(this).remove()});if($(this).val().trim()!=""){var e=validate_fields(c,$(this));if(e.length>0){if(e[0]["message"]!==undefined&&e[0]
["message"]!=""&&e[0]["message"]!="success"){var d='<div class="alert">'+e[0]["message"]+"</div>";$(this).after(d);$(this).siblings
(".alert").fadeIn("fast")}}}})});c.find("#form-captcha-refresh").click(function(){reset_captcha(c)});c.submit(function(e)
{e.preventDefault?e.preventDefault():e.returnValue=false;$(this).find(".form-loader").fadeIn("fast");var d=$(this).attr("action");if
(d===undefined&&d==""){return false}$(this).find(".alert").fadeOut("fast",function(){$(this).remove()});$(this).find(".form-general-
error-container").fadeOut("fast",function(){$(this).empty()});var f=false;$(this).find(".validate-field").each(function(){var
h=validate_fields(c,$(this));if(h.length>0){if(h[0]["message"]!==undefined&&h[0]["message"]!=""&&h[0]["message"]!="success"){var
g='<div class="alert">'+h[0]["message"]+"</div>";$(this).after(g);$(this).siblings(".alert").fadeIn("fast");f=true}}});if(f==true)
{$(this).find(".form-loader").fadeOut("fast");return false}$.ajax({type:"POST",url:d,data:$(this).serialize
(),dataType:"html",success:function(k){c.find(".form-loader").fadeOut("fast");var l=(k=="success")?true:false;var h=(k=="captcha")?
false:true;var g="";switch(k){case"success":g="Form submitted successfully.";break;case"captcha":g="Incorrect text entered. (Case-
sensitive)";break;case"incomplete":g="Please fill in all required fields.";break;case"error":g="An error occured. Please try again
later.";break}var j='<div class="alert ';j+=(l==true)?"success":"error";j+='">'+g+"</div>";if(!h){c.find("#form-captcha").parent
(".form-group").append(j);c.find("#form-captcha").siblings(".alert").fadeIn("fast")}else{c.find(".form-general-error-container").html
(j).fadeIn("fast",function(){$(this).delay(10000).fadeOut("fast",function(){$(this).html("")})})}reset_captcha(c);if(l==true){c.find
(".form-control").val("")}},error:function(h){c.find(".form-loader").fadeOut("fast");var g='<div class="alert">An error occured. Please
try again later.</div>';c.find(".form-general-error-container").html(g).fadeIn("fast")}})})})}function reset_forms(b){if(b!
==undefined&&b.length>0){var a=b;a.find("input").val("");a.find(".alert").remove();a.find(".form-general-error-container").empty
().hide();reset_captcha(b)}}function reset_captcha(b){var a=(b!==undefined&&b.length>0)?b:$("form.validate-form");a.each(function(){var
e=$(this);var c=e.find("#form-captcha-img");if(c.length>0&&e.is(":visible")){var f=new Date().getTime();c.replaceWith('<img id="form-
captcha-img" src="assets/php/form_captcha/captcha_img.php?t='+f+'" style="display:none">');e.find("#form-captcha").val("");setTimeout
(function(){e.find("#form-captcha-img").show()},500)}})}function validate_fields(d,a){if(d!==undefined&&d.length>0){var b=(a!
==undefined&&a.length>0)?a:d.find(".validate");var c=new Array();b.each(function(){var e=$(this).attr("data-validation-type");var
h=$(this).hasClass("required");var g=$(this).val().trim();var f=new Array();f.field_object=$(this);f.message="success";if(h==true&&
(g==""||g===null||g===undefined)){f.message="This field is required"}if(e=="string"&&(g!=""&&g!==null&&g!==undefined)){if(g.match(/^[a
-z0-9 .\-]+$/i)==null){f.message="Invalid characters found."}}else{if(e=="email"&&(g!=""&&g!==null&&g!==undefined)){if(g.match(/^
(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]
+\.)+[a-zA-Z]{2,}))$/)==null){f.message="Please enter a valid email address."}}else{if(e=="phone"&&(g!=""&&g!==null&&g!==undefined))
{if(g.match(/^\(?\+?[\d\(\-\s\)]+$/)==null){f.message="Invalid characters found."}}}}c.push(f)});return c}}function
contact_form_IE9_placeholder_fix(){var a=$("form");a.each(function(){var b=$(this);$(this).find(".form-control").each(function(){var
c=$(this).attr("placeholder");if(c!==undefined&&c!=""){$(this).val(c);$(this).focus(function(){if($(this).val()==c){$(this).val
("")}});$(this).blur(function(){if($(this).val()==""){$(this).val(c)}})}})})}function change_bg_check(){return
((jQuery.browser.mobile&&background_settings.change_on_mobile)||(!jQuery.browser.mobile&&background_settings.change_on_nonmobile))?
true:false}function tabs_uniform_height(){var g=$(".tabpanel.uniform-height");for(var f=0,a=g.length;f<a;f++){var e=0;var h=$(g
[f]).find(".tab-pane");for(var d=0,b=h.length;d<b;d++){var c=$(h[d]).attr("style");$(h[d]).css
({position:"absolute",visibility:"hidden",display:"block"});e=($(h[d]).outerHeight(true)>e)?$(h[d]).outerHeight(true):e;$(h[d]).attr
("style",c?c:"")
}h.css({"min-height":e+"px"})}};
PHP
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from = 'Contact Form';
$to = 'race@ogracing.eu';
$subject = 'Message from website ';
$body = "From: $name\n E-Mail: $email\n Telephone: $phone\n message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if telephone number has been entered
if (!$_POST['phone']) {
$errMessage = 'Please enter a telephone number';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errPhone && !$errMessage) {
if (mail($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
}
else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
更新
我从mail.php找到了一些回复 - 如下所示。这些出现在萤火虫的窗口中!!
回复标题
请求标题
发表
HTML 没有Nada和Nix - 猜这是问题的一部分
缓存 正在发送
所以看来我们没有为电子邮件生成任何HTML,因此没有发送。欢迎提供更多帮助和建议。感谢。
答案 0 :(得分:0)
在您发布的PHP
代码中,您有以下内容:
if (!$errName && !$errEmail && !$errPhone && !$errMessage)
但$errPhone
变量未在任何地方声明。可能在你的日志文件中你会发现一个通知:
Notice: Undefined variable: $errPhone in file_path/file_name.php on line...
并且说你“成功或失败的文字没有出现”我可以说错误在那里,因为Notice
由于你的嵌套{{1}永远不会被执行。
如果您没有日志文件,请尝试在脚本顶部设置if
,您将直接在页面上看到错误。
为了给您一个解决方案,您还必须检查是否存在error_reporting(E_ALL)
并声明变量$_POST["phone"]
(正如您对其他字段$errPhone
,name
和email
)允许message
语句返回if
并继续执行。
此外,正如@CBroe中this comment所述,您必须为提交按钮指定名称:
true
通过这种方式,<button type="submit" name="submit" class="btn btn-sm btn-outline-inverse">Submit</button>
脚本中将提供$_POST['submit']
。