无法从表单php获取textarea数据

时间:2014-03-06 04:02:54

标签: php html forms

我在使用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

消息:

2 个答案:

答案 0 :(得分:1)

我已经测试了你的代码,但我发现了一些错误@ ur电子邮件验证码使用preg_match发送php代码的邮件..当我发表评论时,它有效。

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

更改或使用其他模式检查邮件..

答案 1 :(得分:0)

试试这个:

$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."";