如何将这个php表单POST表格数据发送到我的电子邮箱?

时间:2016-04-30 11:10:25

标签: php forms

我有一个带有以下代码的php文件。当我点击提交时,我似乎遇到了错误。

该错误只是重定向到PHP代码中的错误页面,具体参考"您的姓名"导致错误 但我无法解决如何解决问题。

PHP代码在这里|

<?php
/* Set e-mail recipient */
$myemail = "myemail@live.co.uk";

/* Check all form inputs using check_input function */
$name = check_input($_POST['inputName'], "Your Name");
$email = check_input($_POST['inputEmail'], "Your E-mail Address");
$subject = check_input($_POST['inputSubject'], "Message Subject");
$message = check_input($_POST['inputMessage'], "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, but might not work for drop down subject...just try */

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

$message = "

Someone has sent you a message using your contact form:

Name: $name
Email: $email
Subject: $subject

Message:
$message

";

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

/* Redirect visitor to the thank you page */
header('Location: http://www.address.com/sucesssubmit.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)
{
?>
<html>
<body>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}
?>

表格在这里

<form role="form" id="contact-form" method="post" action="send_form_email.php">
                        <div class="form-group">
                            <label for="name">Your Name</label>
                            <input type="text" name="name" class="form-control" id="name" />
                        </div>
                        <div class="form-group">
                            <label for="email">Email address</label>
                            <input type="email" name="email" class="form-control" id="email" />
                        </div>
                        <div class="form-group">
                            <label for="phone">Phone</label>
                            <input type="text" name="phone" class="form-control" id="phone" />
                        </div>
                        <div class="form-group">
                            <label for="message">Your message</label>
                            <textarea name="message" class="form-control" id="message" rows="6"></textarea>
                        </div>
                        <div class="submit">
                            <button type="submit" class="button button-small" value="Email us" id="btnContactUs" />
                        </div>
</form>

1 个答案:

答案 0 :(得分:0)

使用输入名称访问$ _POST:

/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Your Name");
$email = check_input($_POST['email'], "Your E-mail Address");
$subject = check_input($_POST['inputSubject'], "Message Subject"); // subject is not anymore in html
$message = check_input($_POST['message'], "Your Message");