输入类型textarea通过电子邮件发送为来自php邮件的“未定义”

时间:2010-11-11 11:48:39

标签: php jquery email textarea

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

我上面有这个jquery来验证一个名为“text”的textarea。这与其他值一起,将.ajax发送到php页面发送电子邮件。好的,其他所有内容的电子邮件都很好,但textarea是“未定义”的?有任何想法吗?我需要发布更多代码吗?

编辑:

其余代码:

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>

1 个答案:

答案 0 :(得分:1)

我有类似的问题,我的问题是PHP代码。试试看看#textarea是否正确_POST编辑。

我正在使用它并且对我来说非常有效:

//we need to get our variables first

$email_to =   'xxxxx@yahoo.com'; //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脚本的方式存在问题。

尝试查看jquery可以提供的$.postserialize()函数:

给你一个想法:

    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');
            }
        });