【发布时间】:2013-05-08 19:36:07
【问题描述】:
我正在尝试使用 HTML、PHP 和引导 twitter 在网站 (http://youngliferaffle.com/) 上创建一个表单。我一直在查看有关如何创建它的几个教程,但我认为我在使用位置时遇到了问题——例如在用户出错或提交答案后重新定位用户。我对 PHP 也很陌生。我非常感谢您的帮助!谢谢!
主要问题:
- “重定向到网页的次数过多”/可能是由于 cookie 造成的
- 未收到来自提交的电子邮件
- 提交后或由于提交错误,用户没有被重定向到正确的页面
这是我的 HTML 表单的一部分:
<form method="POST" action="contact-form-submission.php">
<fieldset>
<label><strong>Sign-Up</strong></label>
<input type="text" class="name" name="cname" id="name" placeholder="Full Name"></input>
<input type="text" class="phone" name="phone" id="phone" placeholder="Phone Number"></input>
<input type="text" class="email" name="email" id="email" placeholder="Email Address"></input>
<div class="form-actions">
<input type="submit" name="save" value="Send">
</div>
</fieldset>
</form>
这是我的 PHP 表单
// check for form submission - if it doesn't exist then send back to contact form
if (!isset($_POST['save']) || $_POST['save'] != 'contact') {
header('Location: contact-form-submission.php');
exit;
}
// get the posted data
$name = $_POST['contact_name'];
$email_address = $_POST['contact_email'];
$phone = $_POST['contact_phone'];
// check that a name was entered
if (empty($name))
$error = 'You must enter your name.';
// check that an email address was entered
elseif (empty($email_address))
$error = 'You must enter your email address.';
// check for a valid email address
elseif (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $email_address))
$error = 'You must enter a valid email address.';
// check that a phone number was entered
elseif (empty($phone))
$error = 'You must enter a phone number.';
// check if an error was found - if there was, send the user back to the form
if (isset($error)) {
//Am I putting the wrong location?
header('Location: contact-form-submission.php?e='.urlencode($error)); exit;
}
// write the email content
$email_content = "Name: $name\n";
$email_content .= "Email Address: $email_address\n";
$email_content .= "Phone:\n\n$phone";
// send the email
mail ("myemail.com", "New Contact Message", $email_content);
// send the user back to the form
//And This is where I'm having trouble with! the Location part
header('Location: contact-form-submission.phps='.urlencode('Thank you for your message.'));
exit;
【问题讨论】:
-
那么你有什么具体问题?
-
这个脚本什么时候执行?是在 GET contact-form-submission.php、POST contact-form-submission.php 还是其他 URL 之后?
-
如果您尝试提交表单,即使没有正确输入,它也会将您重定向并说有太多重定向到网页 (youngliferaffle.com/contact-form-submission.php)。此外,即使我填写正确,它也一样,我没有收到电子邮件。
-
这一行 header('Location: contact-form-submission.php?e='.urlencode($error));出口;正在将页面重定向到同一页面。然后它重定向到自己等等等等......
-
@maeusz 我不知道如何或把它放在哪里 ".$_GET['s'].""; // 检查表单错误 elseif (isset($_GET['e'])) echo "".$_GET['e']."";我假设您指的是要放入 html 中?我已经放置了 php 标签,但它仍然会出现在我的 html 页面上
标签: php html forms twitter-bootstrap