【问题标题】:PHP Issue with Contact Form联系表格的 PHP 问题
【发布时间】:2015-06-22 16:34:03
【问题描述】:

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

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

请更正以下错误:输入您的姓名 点击返回按钮,然后重试

即使表单上的“名称”框中有文字。

有什么想法吗?

【问题讨论】:

  • 需要注意的几件事(与主要问题不同):您的代码缺少结束 &lt;/form&gt; 标记。其次,您应该使用验证码来避免通过表单提交垃圾邮件。

标签: php html forms email post


【解决方案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>

【讨论】:

  • 谢谢。这有帮助。表单现在正在发送我现在遇到的问题是“警告:无法修改标头信息 - 标头已由 /homepages/5/d432350608/htdocs/fleetmotus/beta/mailer.php:7 中的 /homepages/ 5/d432350608/htdocs/fleetmotus/beta/mailer.php 第 38 行“有什么想法吗?
  • 页眉前有一些数据打印,确保页眉前没有任何内容。
【解决方案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 -->

【讨论】:

    猜你喜欢
    • 2013-08-15
    • 2016-06-23
    • 1970-01-01
    • 2013-11-02
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多