PHP联系表格问题

时间:2015-04-16 10:22:42

标签: php html forms email post

PHP联系表格有问题。

<form action="contactform.php" id="contact-form">
<div id="success"></div>
<ul>
<li class="input-name">
    <input type="text" id="name" class="required" placeholder="Name">
</li> <!-- END input-name -->
<li class="input-email">
    <input type="text" id="email" class="email" placeholder="Email Address">
</li> <!-- END input-name -->
<li class="input-subject">
    <input type="text" id="subject" placeholder="Subject">
</li> <!-- END input-name -->
<li class="input-subject">
    <textarea rows="7" cols="50" id="message" class="required" placeholder="Message"></textarea>
</li> <!-- END input-name -->
</ul> <!-- END #contact -->
<button type="submit" class="btn btn-primary btn-lg pull-right">Send Message</button>

PHP的内容是

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

/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$subject = check_input($_POST['subject'], "Enter a subject");
$email = check_input($_POST['email']);
$message = check_input($_POST['message'], "Write your message");

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

Name: $name
E-mail: $email
Subject: $subject

Message:
$message

";

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

/* Redirect visitor to the thank you page */
header('Location: thanks.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();
}
?>

当我尝试测试表单时,我得到了

  

请更正以下错误:输入您的姓名   点击后退按钮再试一次

即使&#34;姓名&#34;表格上的方框。

任何想法?

2 个答案:

答案 0 :(得分:2)

您的html中没有设置任何名称属性:

<li class="input-name">
    <input type="text" id="name" name="name" class="required" placeholder="Name">
</li> <!-- END input-name -->
<li class="input-email">
    <input type="text" id="email" name="email" class="email" placeholder="Email Address">
</li> <!-- Etc Etc -->

答案 1 :(得分:1)

表单中缺少

method="",并且所有输入中都缺少name=""属性,请使用下面的表单。

<form action="contactform.php" method="post" id="contact-form">
<div id="success"></div>
<ul>
<li class="input-name">
    <input type="text" id="name" name="name" class="required" placeholder="Name">
</li> <!-- END input-name -->
<li class="input-email">
    <input type="text" id="email" name="email" class="email" placeholder="Email Address">
</li> <!-- END input-name -->
<li class="input-subject">
    <input type="text" id="subject" name="subject" placeholder="Subject">
</li> <!-- END input-name -->
<li class="input-subject">
    <textarea rows="7" cols="50" id="message" name="message" class="required" placeholder="Message"></textarea>
</li> <!-- END input-name -->
</ul> <!-- END #contact -->
<button type="submit" class="btn btn-primary btn-lg pull-right">Send Message</button>