0

我正在按照以下教程使用 PHP、AJAX 和 JQUERY 在我的网站上放置一个表单,这会将表单信息发送到我的电子邮件:

http://www.spruce.it/noise/simple-ajax-contact-form/

问题是,当我准备好文档外的 jquery 时,我根本没有收到任何消息,当我将它放在准备好的文档中时,我得到了错误文本,但是当字段中有信息时,什么也没有发生。拜托,有人可以看看我的 html、jquery、php 或 AJAX 可能有什么问题吗?我要拔掉我所有的头发。我正在 Wampserver 中对其进行测试。

HTML 文件位于 PHP 文件所在的根目录中。在根目录中有一个名为“includes”的文件夹,其中包含 Javascript。以下是每个文件夹的相关代码:

HTML:

<form id="repairform" method="post"> 
   <p id="p1">Name:</p> 
   <input id="one" type="text" name="name" /> 

   <p id="p2">How would you prefer to be reached?: </p> 
   <select id="two" name="Contact methods">
      <option value="Phone">Email</option>
      <option value="Email">Phone</option>
   </select> 

   <p id="p3">What kind of computer are you having trouble with?</p>
   <p id="p3-2">Give as much or as little info. as you'd like.</p>
   <p id="p3-3">(Laptop PC, desktop Macintosh, etc)</p>
   <textarea id="four" name="pc type" rows="3" cols="30"></textarea>

   <p id="p4">What problems are you having with your computer/ what needs to be fixed?</p>
   <textarea id="five" name="problem" rows="5" cols="30"></textarea>
   <input id="three" type="submit" value="Submit Request" />

   <p id="p5">What is your Email?</p> 
   <input id="six" type="text" name="Email/Phone" /> 

   <p id="p7">What is your Phone Number?</p>
   <input id="eight" type="text" name="Email/Phone2" /> 

   <p id="p6">What time of day would you prefer to be reached?</p>
   <input id="seven" type="text" name="Preferred Contact Time" /> 
</form>

查询:

$(document).ready(function () {

   $("#repairform").submit(function (e) {
      e.preventDefault();

      if (!$("#six").val()) {
         $("#six").val("shanew@ufl.edu");
      }

      var name = $("#one").val();
      var email = $("#six").val();
      var text = $("#five").val();
      var reachpreference = $("#two").val();
      var computertype = $("#four").val();
      var phonenumber = $("#eight").val();
      var timeofday = $("#seven").val();

      var dataString = 'name=' + name + '&email=' + email + '&text=' + text
      + '&reachpreference=' + reachpreference + '&computertype=' + computertype
      + '&phonenumber=' + phonenumber + '&timeofday=' + timeofday;

      function isValidEmail(emailAddress) {
         var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
         return pattern.test(emailAddress);
      };

      if (isValidEmail(email) && (text.length > 2) && (name.length > 1)) {
         $.ajax({
            type: "POST",
            url: "../functions.php",
            data: dataString,
            success: function () {
               alert("Thank you! Your message has been delivered. I will be back with you shortly");
            }
         });
      } else {
         alert("Some of the form information was not filled out correctly. Ensure all of the correct fields are filled out.");
      }

      return false;
   });

PHP:

 <?php
 // Email Submit
 if (isset($_POST['email']) && isset($_POST['name']) && isset($_POST['text'])){

    //send email
    mail("shanew@ufl.edu", "Contact Form: ".$_POST['name'],
    $_POST['text'], $_POST['reachpreference'], $_POST['computertype'] 
    $_POST['phonenumber'], $_POST['timeofday'], "From:" . $_POST['email']);

 }
 ?>
4

2 回答 2

1

利用

data: $('#repairform').serializeArray()

而不是datastring您正在创建的对象。

datastring被视为字符串,您将永远无法使用$_POST['text']all 访问它。您可以尝试使用 using$_GET代替。将datastring只能以这种方式访问​​。

于 2013-05-15T17:04:13.853 回答
0

我认为你错过了一些关闭分支}) ;

而且我认为您应该将name属性用于将在 php 中使用的变量名称。

<form id="theForm">
    <input type="text" name="email" />
</form>

在 javascript 中,您可以使用序列化来减少行数并且更易于阅读。

$.ajax({
    type:'POST'
    url:'../functions.php'
    data:$('#theForm').serialize();
})

并在 php

echo $_POST['email']
于 2013-05-15T17:16:43.473 回答