2010-11-11 35 views
1
var text = $("input#text").val(); 
    if (text == "") { 
      $("input#text").focus(); 
      alert("Please complete all fields"); 
      return false; 
    } 

我有这个jquery上面来验证一个叫做“文本”的textarea。这和其他值一起,将.ajax发送到一个发送电子邮件的php页面。电子邮件通过罚款与其他一切正常,但textarea通过“未定义”?有任何想法吗?我是否需要发布更多的代码?输入类型textarea来通过电子邮件作为“undefined”从PHP邮件

编辑:

休息代码:

php的:

     $email = $_REQUEST['email'] ; 
         $text = $_REQUEST['text'] ; 
         $name = $_REQUEST['name'] ; 
         $detail = "Name: ".$name."\nMessage: ".$text; 
         mail("xxxxxxxxx", "Subject: Contact Form", 
         $detail, "From: $email"); 
         echo "Thank you for getting in touch"; 

完整的jQuery:

$(function() { 

$( '#提交')生活(”。点击',功能(){

var name = $("input#name").val(); 
    if (name == "") { 
      $("input#name").focus(); 
      alert("Please complete all fields"); 
      return false; 
    } 

    var email = $("input#email").val(); 
    if (email == "") { 
      $("input#email").focus(); 
      alert("Please complete all fields"); 
      return false; 
    } 

    var text = $("input#text").val(); 
    if (text == "") { 
      $("input#text").focus(); 
      alert("Please complete all fields"); 
      return false; 
    } 

    var dataString = 'name=' + name + '&email=' + email + '&text=' + text; 
    //alert (dataString);return false; 

    $.ajax({ 
    type: "POST", 
    url: "mailform.php", 
    data: dataString, 
    success: function() { 
    alert("Thanks, we will be in touch soon"); 
    } 
}); 
return false; 
}); 

});

的HTML:

<form method='post' action='mailform.php' class="form"> 

       <p class="name"> 
       <label for="name">Name</label> 
        <input type="text" name="name" id="name" />  
       </p> 

       <p class="email"> 
        <label for="email">E-mail</label> 
        <input type="text" name="email" id="email" />    
       </p> 

       <p class="text"> 
        <label for="text">Nature of Enquiry</label> 
        <textarea id="text" name="text"></textarea> 
       </p> 

       <p class="submit"> 
        <input type="submit" id="submit" value="Send" /> 
       </p> 

      </form> 
+0

您可以在发送请求的位置添加javascript行吗?到目前为止,一切都很好。 – arby 2010-11-11 12:00:15

回答

1

我有一个类似的问题,我的问题是用PHP代码。尝试哟看看有没有看到#textarea是否正确得到_POST - 编辑。

我用这和完美的作品对我来说:

//we need to get our variables first 

$email_to = '[email protected]'; //the address to which the email will be sent 
$name  = $_POST['name']; 
$email = $_POST['email']; 
$subject = $_POST['subject']; 
$message = $_POST['message']. "\n\n"; 

/*the $header variable is for the additional headers in the mail function, 
we are asigning 2 values, first one is FROM and the second one is REPLY-TO. 
That way when we want to reply the email gmail(or yahoo or hotmail...) will know 
who are we replying to. */ 
$headers = "From: $email\r\n"; 
$headers .= "Reply-To: $email\r\n"; 

if(mail($email_to, $subject, $message, $headers)){ 
    echo 'sent'; // we are sending this text to the ajax request telling it that the mail is sent..  
}else{ 
    echo 'failed';// ... or this one to tell it that it wasn't sent  
} 

不知道发生了什么事情,像其他用户说我认为是与数据被结转到PHP的方式有问题脚本。

尝试寻找到$.postserialize()功能jQuery可提供:

给你一个想法:

var error = false; 
    var name = $('#name').val(); 
    var email = $('#email').val(); 
    var subject = $('#subject').val(); 
    var message = $('#message').val(); 

---由于一些检查,看看是否的信息是正确的---

if(error == false){ 
     //#contact_form has all the variables that get serialized and sent to the php 
      and you can get a message back to check if everything went okay. 
     $.post("your_php_code.php", $("#contact_form").serialize(),function(result){ 
      //and after the ajax request ends we check the text returned 
      if(result == 'sent'){ 
       //if the mail is sent remove the submit paragraph 
       $('#cf_submit_p').remove(); 
       //and show the mail success div with fadeIn 
       $('#mail_success').fadeIn(500); 
      }else{ 
       //show the mail failed div 
       $('#mail_fail').fadeIn(500); 
       $('#send_message').removeAttr('disabled').attr('value', 'Send The Message'); 
      } 
     }); 
+0

谢谢,在Firebug中,“文字”在帖子中是“未定义的”!任何想法 – benhowdle89 2010-11-11 12:05:55

+0

我添加了一些与联系表单一起使用的代码。不知道该说什么关于你的代码的问题。 – Chris19 2010-11-11 12:23:36

+0

什么打印“<! - ”。 htmlentities(var_export($ _ POST,true))。 “ - >”;显示? – symcbean 2010-11-11 12:28:24