我正在尝试使用 HTML、PHP 和 bootstrap 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;