gpt4 book ai didi

php - AJAX 联系表单在 IE 中不起作用

转载 作者:行者123 更新时间:2023-11-28 21:07:15 25 4
gpt4 key购买 nike

我已经发布了一篇关于如何创建 ajax 联系表单的教程。该教程可在此处获取:
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

现在它可以工作了,我真的很喜欢它,但由于某种原因,它无法在任何版本的 Internet Explorer 中工作(在 Chrome、Firefox 和 Opera 中测试过,工作正常)。我真的不知道是什么导致了这个问题。

您可以在此处测试联系表:http://freshbeer.lv/new/latvian/contact.php (它是拉脱维亚语,第一个字段是您的姓名,第二个字段是您的电子邮件,第三个字段是您的电话(不需要),第四个字段是您的消息)

这是此表单的 jQuery:

$(function() {
$('.error').hide(); //Hide error message

$(".button").click(function() {
$('.error').hide();

var name = $("input#name").val(); //Check if name is not empty
if (name == "") {
$("label#name_error").show();
$("input#name").focus();
return false;
}

function isValidEmailAddress(emailAddress) { //Function to check e-mail
var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][\d]\.|1[\d]{2}\.|[\d]{1,2}\.))((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\.){2}(25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\]?$)/i);
return pattern.test(emailAddress);
};

var email = $("input#email").val();//Check email with function
if (!isValidEmailAddress(email)) {
$("label#email_error").show();
$("input#email").focus();
return false;
}

var letter = $("textarea#letter").val(); Check if message was entered
if (letter == "") {
$("label#letter_error").show();
$("textarea#letter").focus();
return false;
}

var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&letter=' + letter; //Build Data String

$.ajax({ //Ajas post data to process.php script
type: "POST",
url: "../scripts/process.php",
data: dataString,
success: function() { //Display success message
$('#contact_form').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>")
.append("<p>We will be in touch soon.</p>")
}
});
return false;
});
});

这里是 php (process.php),PHP Mailer 用于发送电子邮件

<?php
if ((isset($_POST['name'])) && (strlen(trim($_POST['name'])) > 0)) {
$name = stripslashes(strip_tags($_POST['name']));
} else {$name = 'No name entered';}
if ((isset($_POST['email'])) && (strlen(trim($_POST['email'])) > 0)) {
$email = stripslashes(strip_tags($_POST['email']));
} else {$email = 'No email entered';}
if ((isset($_POST['phone'])) && (strlen(trim($_POST['phone'])) > 0)) {
$phone = stripslashes(strip_tags($_POST['phone']));
} else {$phone = 'No phone entered';}
if ((isset($_POST['letter'])) && (strlen(trim($_POST['letter'])) > 0)) {
$letter = stripslashes(strip_tags($_POST['letter']));
} else {$letter = 'No Message';}
ob_start();
?>
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<table width="550" border="1" cellspacing="2" cellpadding="2">
<tr bgcolor="#eeffee">
<td>Name</td>
<td><?=$name;?></td>
</tr>
<tr bgcolor="#eeeeff">
<td>Email</td>
<td><?=$email;?></td>
</tr>
<tr bgcolor="#eeffee">
<td>Phone</td>
<td><?=$phone;?></td>
</tr>
<tr bgcolor="#eeeeff">
<td>Message</td>
<td><?=$letter;?></td>
</tr>
</table>
</body>
</html>
<?
$body = ob_get_contents();

require("phpmailer.php");

$mail = new PHPMailer();

$mail->From = "mail@freshbeer.com";
$mail->FromName = "Bryuvers";
$mail->AddAddress("my@email.com","Name 1"); //new mail

$mail->WordWrap = 50;
$mail->IsHTML(true);

$mail->Subject = "New Message!";
$mail->Body = $body;
$mail->AltBody = "Message from website contact form";

if(!$mail->Send()) {
$recipient = 'my@email'; //new mail
$subject = 'Contact form failed';
$content = $body;
mail($recipient, $subject, $content, "From: mail@freshbeer.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
exit;
}
?>

这是表单的 HTML

 <form name="contact" action="">
<fieldset>
<input placeholder="Jusu vards, uzvards (Obligati)" type="text" name="name" id="name" class="text-input" /><br />
<label class="error" for="name" id="name_error"><b>Ludzu noradiet jusu vardu, uzvardu!</b></label><br />

<input placeholder="e-pasta adrese (Obligati)" type="text" name="email" id="email" class="text-input" /><br />
<label class="error" for="email" id="email_error"><b>Ludzu noradiet jusu e-pastu!</b></label><br />

<input placeholder="Telefona Nr." type="text" name="phone" id="phone" class="text-input" /><br /><br />

<textarea placeholder="Jautajums (Obligati)" name="letter" id="letter" class="text-input" /></textarea><br />
<label class="error" for="letter" id="letter_error"><b>Ludzu uzrakstiet jusu jautajumu!</b></label><br />

<input type="submit" name="submit" class="button" id="submit_btn" value="Nosutit" />
</fieldset>
</form>

最佳答案

javascript 变量“phone”未定义。

尝试添加

 var phone = $("#phone").val();

编辑:根据建议,直接使用 $("#phone")

关于php - AJAX 联系表单在 IE 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9587552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com