0
votes

I am having an issue getting the textarea data from html form using php. All of the other data on the form gets emailed to me but the message data is blank. form validates fine.

I don't think I have a syntax error, maybe another eye on something this simple could assist.

Here is my full form that I validate with 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>

Here is my php script handing the form data and emailing it.

<?php
/* Set e-mail recipient */
$myemail = "[email protected]";

/* 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();
}
?>

Here is the content of the email I am getting.(or was getting before messing around trying to fix the textarea issue

Someone has sent you a message using your contact form:

Name: sdfgsdfg Email: [email protected] Company: sdfgsdfg Website: http://sdfgdsfg.com

Message:

2
your TextArea is out the 'Form' tagjondinham
please post your full code.عثمان غني
try var_dump($_POST); and see if you're really getting the post variable msgViscocent

2 Answers

1
votes

i have tested your code but i found some mistake @ ur email validation code using preg_match on your mail sending php code..when i comment it, it works.

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

do change that or use another pattern for checking mail..

0
votes

Try this one:

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