【问题标题】:unable to get textarea data from form php无法从表单 php 获取 textarea 数据
【发布时间】:2014-04-08 12:11:22
【问题描述】:

我在使用 php 从 html 表单获取 textarea 数据时遇到问题。表单上的所有其他数据都会通过电子邮件发送给我,但消息数据是空白的。表单验证正常。

我不认为我有语法错误,也许换个眼睛看看这个简单的东西会有所帮助。

这是我用 jquery 验证的完整表单。

<div class="container" id="contact">
            <!-- form: -->
            <div class="panel panel-default">
                    <div class="panel-heading">
                        <h2 class="panel-title">Contact Us / Request a Quote</h2>
                    </div>


                    <div class="panel-body">


                    <form id="defaultForm" method="post" class="form-horizontal" action="email2.php">
                        <div class="form-group">
                            <label class="col-lg-2 control-label ">Name</label>
                    <div class="col-lg-10">
                            <input type="text" class="form-control" name="name" />
                            </div>
                </div>
                        <div class="form-group">
                            <label class="col-lg-2 control-label">Email address</label>
                    <div class="col-lg-10">
                            <input type="text" class="form-control" name="email" />
                            </div>
                        </div>
                <div class="form-group">
                            <label class="col-lg-2 control-label">Company</label>
                    <div class="col-lg-10">
                            <input type="text" class="form-control" name="company" />
                            </div>

                        </div>
                <div class="form-group">
                            <label class="col-lg-2 control-label">Website (include http://)</label>
                    <div class="col-lg-10">
                            <input type="text" class="form-control" name="url" />
                            </div>

                        </div>
                        <div class="form-group">
                            <label class="col-lg-2 control-label">Message (please include as much detail as possible</label>
                    <div class="col-lg-10">
                            <textarea class="form-control" rows="5" name="msg"></textarea>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="control-label col-lg-2" id="captchaOperation"></label>
                            <div class="col-lg-10">
                            <input type="text" class="form-control" name="captcha" />
                            </div>
                        </div>
                        <div class="form-group">
                                <label class="control-label col-lg-2"></label>
                                <div class="col-lg-10">
                                    <button type="submit" id="alertMe" class="btn btn-primary">
                                        Send Message
                                    </button>
                                </div>
                        </div>
                    </form>
            </div>
             </div>   
            <!-- :form -->
        </div>
    </div>

<script type="text/javascript">
$(document).ready(function() {
    // Generate a simple captcha
    function randomNumber(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    };
    $('#captchaOperation').html([randomNumber(1, 10), '+', randomNumber(1, 20), '='].join(' '));

    $('#defaultForm').bootstrapValidator({
        message: 'This value is not valid',
        fields: {
            name: {
                message: 'The username is not valid',
                validators: {
                    notEmpty: {
                        message: 'You forgot your name'
                    },
                    stringLength: {
                        max: 40,
                        message: 'Name must be less than 40 characters long'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z ]+$/,
                        message: 'The Name can only consist of alphabetical characters'
                    },

                }
            },
            email: {
                validators: {
                    emailAddress: {
                        message: 'Please enter a valid email address'
                    }
                }
            },
            company: {
                validators: {
                    notEmpty: {
                        message: 'Please enter your company name'
                    }
                }
            },
            url: {
                validators: {
                    uri: {
                        message: 'Please enter a valid url'
                    }
                }
            },
            msg: {
                validators: {
                    notEmpty: {
                        message: 'Please enter a message with as much detail as possible'
                    }
                }
            },
            captcha: {
                validators: {
                    callback: {
                        message: 'Wrong answer',
                        callback: function(value, validator) {
                            var items = $('#captchaOperation').html().split(' '), sum = parseInt(items[0]) + parseInt(items[2]);
                            return value == sum;
                        }
                    }
                }
            }
        }
    });
});
</script>

这是我处理表单数据并通过电子邮件发送的 php 脚本。

<?php
/* Set e-mail recipient */
$myemail = "test@test.com";

/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Your Name");
$email = check_input($_POST['email'], "Your E-mail Address");
$website = check_input($_POST['url'], "Your website");
$company = check_input($_POST['company'], "Company");
$msg = check_input($_POST['msg'], "Your Message");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */

$subject = "Someone has sent you a message";

$message = "

Someone has sent you a message using your contact form:

Name: $name
Email: $email
Company: $company
Website: $url

Message:
$msg

";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: contact-ty.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>

<?php
exit();
}
?>

这是我收到的电子邮件的内容。(或者在尝试解决 textarea 问题之前收到的电子邮件

有人使用您的联系表向您发送了一条消息:

名称:sdfgsdfg 邮箱:test@email.com 公司:sdfgsdfg 网址:http://sdfgdsfg.com

消息:

【问题讨论】:

  • 您的 TextArea 不在“表单”标签中
  • 请发布您的完整代码。
  • 尝试 var_dump($_POST);看看你是否真的得到了 post 变量 msg

标签: php html forms


【解决方案1】:

试试这个:

$message = "Someone has sent you a message using your contact form: \n \n    Name: ".$name." \n Email: ".$email." \n Company: ".$company." \n Website: ".$url." \n Message:".$msg."";

【讨论】:

    【解决方案2】:

    我已经测试了你的代码,但我发现了一些错误@你的电子邮件验证代码在你的邮件发送 php 代码上使用 preg_match..当我评论它时,它可以工作。

    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
    {
    show_error("Invalid e-mail address");
    }
    

    改变它或使用其他模式来检查邮件..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-27
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多