我不熟悉Stack溢出和PHP - 我在联系表单上遇到了一些麻烦。它正在使用Mail功能,我会在用户提交表单后收到电子邮件。但是,由于某种原因,提交表单后“消息已发送”声明仅显示在Firefox上,而不是Chrome或Safari上。这是为什么?任何有关这方面的帮助将不胜感激!
这是我的代码:
<?php
$the_blogname = 'blog's name';
$the_myemail = 'my email';
if(isset($_POST['email'])){
error_reporting(0);
$errorC = false;
$the_email = $_POST['email'];
$the_name = $_POST['yourname'];
$the_message = $_POST['message'];
$the_phone = $_POST['phone'];
$the_fax = $_POST['fax'];
$the_company = $_POST['company'];
$the_website = $_POST['website'];
# want to add aditional fields? just add them to the form in template_contact.php,
# you dont have to edit this file
//added fields that are not set explicit like the once above are combined and added before the actual message
$already_used = array('email','yourname','message','phone','fax','company','website','myblogname','tempcode','temp_url','ajax');
$attach = '';
foreach ($_POST as $key => $field)
{
if(!in_array($key,$already_used))
{
$attach.= $key.": ".$field."<br /> \n";
}
}
$attach.= "<br /> \n";
if(!checkmymail($the_email))
{
$errorC = true;
$the_emailclass = "error";
}else{
$the_emailclass = "valid";
}
if($the_message == "")
{
$errorC = true;
$the_messageclass = "error";
}else{
$the_messageclass = "valid";
}
if($errorC == false)
{
$to = $the_myemail;
$subject = "New Message from " . $the_blogname;
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$header .= 'From:'. $the_email . " \r\n";
$message1 = nl2br($the_message);
if(!empty($the_name)) $the_name = "Name: $the_name <br/>";
if(!empty($the_company)) $the_company = "Company: $the_company <br/>";
if(!empty($the_phone)) $the_phone = "Phone: $the_phone <br/>";
if(!empty($the_fax)) $the_fax = "Fax: $the_fax <br/>";
if(!empty($the_website)) $the_website = "Website: $the_website <br/>";
$message = "
You have a new message! <br/>
$the_name
$the_company
$the_phone
$the_website
$attach <br />
Message: $message1 <br />";
if(@mail($to,$subject,$message,$header)) $send = true; else $send = false;
if(isset($_POST['ajax']))
if ($send)
echo '<h3>Your message has been sent!</h3><div class="confirm">
<p class="textconfirm">Thank you for contacting us.<br/>We will get back to you as soon as possible.</p>
</div>';
else
echo '<h3>Oops!</h3><div class="confirm">
<p class="texterror">Due to an unknown error, your form was not submitted, please resubmit it or try later.</p>
</div>';
}
}
function checkmymail($mailadresse){
$email_flag=preg_match("!^\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$!",$mailadresse);
return $email_flag;
}
?>
这是JS(以防万一):
jQuery(document).ready(function(){
tfuse_custom_form();
});
function tfuse_custom_form(){
var my_error;
var url = jQuery("input[name=temp_url]").attr('value');
jQuery("#send").bind("click", function(){
my_error = false;
jQuery(".ajax_form input, .ajax_form textarea, .ajax_form radio, .ajax_form select").each(function(i)
{
var surrounding_element = jQuery(this);
var value = jQuery(this).attr("value");
var check_for = jQuery(this).attr("id");
var required = jQuery(this).hasClass("required");
if(check_for == "email"){
surrounding_element.removeClass("error valid");
baseclases = surrounding_element.attr("class");
if(!value.match(/^\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$/)){
surrounding_element.attr("class",baseclases).addClass("error");
my_error = true;
}else{
surrounding_element.attr("class",baseclases).addClass("valid");
}
}
if(check_for == "message"){
surrounding_element.removeClass("error valid");
baseclases = surrounding_element.attr("class");
if(value == "" || value == "Write your message..."){
surrounding_element.attr("class",baseclases).addClass("error");
my_error = true;
}else{
surrounding_element.attr("class",baseclases).addClass("valid");
}
}
if(required && check_for != "email" && check_for != "message"){
surrounding_element.removeClass("error valid");
baseclases = surrounding_element.attr("class");
if(value == ""){
surrounding_element.attr("class",baseclases).addClass("error");
my_error = true;
}else{
surrounding_element.attr("class",baseclases).addClass("valid");
}
}
if(jQuery(".ajax_form input, .ajax_form textarea, .ajax_form radio, .ajax_form select").length == i+1){
if(my_error == false){
jQuery(".ajax_form").slideUp(400);
var $datastring = "ajax=true";
jQuery(".ajax_form input, .ajax_form textarea, .ajax_form radio, .ajax_form select").each(function(i)
{
var $name = jQuery(this).attr('name');
var $value = encodeURIComponent(jQuery(this).attr('value'));
$datastring = $datastring + "&" + $name + "=" + $value;
});
jQuery(".ajax_form #send").fadeOut(100);
jQuery.ajax({
type: "POST",
url: url+"/sendmail.php",
data: $datastring,
success: function(response){
jQuery(".ajax_form").before("<div class='ajaxresponse' style='display: none;'></div>");
jQuery(".ajaxresponse").html(response).slideDown(400);
jQuery(".ajax_form #send").fadeIn(400);
jQuery(".ajax_form input, .ajax_form textarea, .ajax_form radio, .ajax_form select").val("");
}
});
}
}
});
return false;
});
}