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

标签 php javascript jquery ajax internet-explorer

我已经发布了一篇关于如何创建 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/

相关文章:

php - 如何找出运行 PHP 的进程? (Litespeed/Centos环境)

php - 使用户输入对数据库安全的最佳功能是什么?

jquery - 将单选按钮转换为开/关切换 Bootstrap

javascript - Jquery TubePlayer 插件未按预期运行

javascript - jQuery Loudev 多重选择不起作用

php - 有没有办法在购物车中显示缺货消息,以具体指示哪些商品已售完?

javascript - CodeIgniter - 如何从 Controller 返回 Json 响应

javascript - 使用 JavaScript Math.pow 计算 Excel 公式

javascript - 为什么标签/文本没有在此 d3.js 散点图上呈现?

javascript - 如何根据先前的输入类型范围值创建 'x' 数量的输入类型文本?