【问题标题】:input type textarea coming through to email as "undefined" from php mail输入类型 textarea 来自 php 邮件的“未定义”电子邮件
【发布时间】:2011-05-08 10:21:59
【问题描述】:
var text = $("input#text").val();
    if (text == "") {
          $("input#text").focus();
          alert("Please complete all fields");
          return false;
    }

我在上面有这个 jquery 来验证一个名为“text”的文本区域。这与其他值一起,将 .ajax 发送到用于发送电子邮件的 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() {

$('#submit').live('click',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>

【问题讨论】:

  • 您可以在发送请求的地方添加 javascript 行吗?到目前为止,一切看起来都还不错。

标签: php jquery email textarea


【解决方案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');
            }
        });

【讨论】:

  • 谢谢,在 Firebug 中,帖子中的“文本”是“未定义”!?任何想法
  • 我添加了更多用于联系表单的代码。不知道该怎么说你的代码问题。
  • 还有什么打印 "";显示?
  • 感谢这个 Chris19 - 最后解决了这个问题: var text = $("input#text").val();应该是 textarea#text
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-19
  • 2013-09-19
  • 1970-01-01
  • 1970-01-01
  • 2017-04-13
  • 1970-01-01
  • 2012-04-29
相关资源
最近更新 更多