我正在尝试使用HTML, PHP和bootstrap twitter在网站(http://youngliferaffle.com/)上创建表单。我一直在寻找关于如何创建它的几个教程,但我认为我在位置上遇到了麻烦——如果他们犯了错误或在他们提交答案后重新定位用户。我也是PHP的新手。我真的很感激你的帮助!谢谢你!
主要问题:- "太多重定向到网页"/可能是由于cookie
- 未收到提交的电子邮件
- 用户在提交后没有被重定向到正确的页面或由于错误的提交
<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;
最后一行
header('Location: contact-form-submission.phps='.urlencode('Thank you for your message.')); exit;
似乎你的名字是对的。为什么要将php文件重定向到自身。您应该使用初始表单的URL,不管它的名称是什么。这一行可能会导致重定向循环错误。
php代码中的第一件事是它重定向到自己。'save'变量不会设置,所以它会不断地重定向。它检查'save'是否设置,但第一次没有设置,所以它再次重定向到同一页面。但是'save'变量不会被再次设置因为它只是一个重定向,而不是表单提交。所以它会一次又一次地发生,所以你会得到太多的重定向错误。
我通常将处理逻辑和表单保存在同一个PHP文件中。这意味着,表单的action属性将具有与value相同的页面URL。例如:simple_form.php
<?php
//Assume the form has one field called field1 and a 'save' variable to indicate form submission
$field1 = "";
//Declare an array to store errors
$errors = array();
if(isset($_POST['save'])) {
//Form has been submitted.. do validations etc.
$field1 = $_POST['field1'];
if(someValidationCheck($field1) == false) {
$errors[] = "Field1 is not valid";
}
//After all field validations.. adding errors to $errors array..
if(count($errors) == 0) {
//No errors so write database insert statments etc. here
//Also put a header("Location:...") redirect here if you want to redirect to a thank you page etc.
}
}
?>
<html>
<body>
<?php
//If there were errors, show them here
if(count($errors) > 0) {
//loop through $errors array .. print one by one.
}
?>
<form method="post" action="simple_form.php">
<input type="text" name="field1" value="<?php echo($field1); ?>" />
<input type="hidden" name="save" value="save" />
<input type="submit" />
</form>
</body>
</html>
这样,如果有错误,用户将在同一页面中看到错误消息,字段也将保留其原始值。它只会在没有错误的有效提交时被重定向。否则,它将停留在同一页面,显示错误消息。
在第一行中,您继续返回相同的位置。这就形成了一个循环。你需要把它们送回表格。可能是index . php ?
最后一行,因为这个页面只在发布数据时才会起作用,所以您需要将用户从这个页面重定向。也许可以做一个新的感谢页。
还记得你的?.php之后的?告诉你的web服务器它不再是一个文件名。否则,它将查找名为thankyou.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: index.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
// remember the ? after your file name!
header('Location: thankyou.php?s='.urlencode('Thank you for your message.')); exit;