我创建了一个基于http://rosstanner.co.uk/2012/11/build-simple-contact-form-html-php-bootstrap/的 html / php 表单
它成功验证表单并将发送邮件。但是,如果表单无效,它会将用户返回到 index.php,但会丢失他们输入的表单值,这显然是一个糟糕的用户体验。
我尝试使用 cookie 来纠正这个问题,但我觉得它的做法很糟糕,而且它似乎在 iphone 上也不起作用。有什么建议可以实现吗?
索引.php
<h3><a name="book">Send a booking enquiry</a></h3>
<p>We are in the process of building a new booking system. In the mean
time please enter your details below and you will be contacted
shortly.</p>
<?php
$return_form_vals = array();
if (isset($_COOKIE['cookie'])) {
foreach ($_COOKIE['cookie'] as $name => $value) {
$return_form_vals[htmlspecialchars($name)]=htmlspecialchars($value);
}
}
?>
<div class="boostrap form item">
<form action="contact-send.php" method="post">
<fieldset>
<input class="textfield" name="your_name" placeholder=
"Enter your name" value="<?php echo $return_form_vals['your_name'];?>" type="text"><br>
<input class="textfield" name="phone_number" placeholder=
"Enter your phone number" type="text" value="<?php echo $return_form_vals['phone_number'];?>"><br>
<input class="textfield" name="email_address" placeholder=
"Enter your email address" type="text" value="<?php echo $return_form_vals['email_address'];?>"><br>
<input class="textfield" name="number_of_people" placeholder=
"Enter the number of people in your group" type="text" value="<?php echo $return_form_vals['number_of_people'];?>"><br>
<input id="start_date" class="textfield" type="date" name="start_date" placeholder=
"Enter the start date for your trip" type="text" value="<?php echo $return_form_vals['start_date'];?>"><br>
<input id="end_date" class="textfield" type="date" name="end_date" placeholder=
"Enter the end date for your trip" type="text" value="<?php echo $return_form_vals['end_date'];?>"><br>
<input name="save" type="hidden" value="contact"> <button class=
"btn btn-green" type="submit">Send</button>
</fieldset>
</form><?php
// check for a successful form post
if (isset($_GET['s'])){print_r($_GET['s']); echo "<div class=\"alert alert-success\">".$_GET['s']."</div>"; }
// check for a form error
elseif (isset($_GET['e'])) {echo "<div class=\"alert alert-error\">".$_GET['e']."</div>"; }
?>
</div>
联系-send.php
<?php
if(!isset($_POST['save']) || $_POST['save'] != 'contact')
{
header('Location: index.php');
exit;
}
$your_name = $_POST['your_name'];
$phone_number = $_POST['phone_number'];
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
$number_of_people = $_POST['number_of_people'];
$email_address = $_POST['email_address'];
/*$form_values = array();
$form_values['name'] = $your_name;
$form_values['phone_number'] = $phone_number;
$form_values['start_date'] = $start_date;
$form_values['end_date'] = $end_date;
$form_values['number_of_people'] = $number_of_people;
$form_values['email_address'] = $email_address;*/
setcookie("cookie[your_name]",$_POST['your_name'],time()+10 );
setcookie("cookie[phone_number]",$_POST['phone_number'],time()+10 );
setcookie("cookie[start_date]",$_POST['start_date'],time()+10 );
setcookie("cookie[end_date]",$_POST['end_date'],time()+10 );
setcookie("cookie[number_of_people]",$_POST['number_of_people'],time()+10 );
setcookie("cookie[email_address]",$_POST['email_address'],time()+10 );
if(empty($your_name))
{
$error = 'You must enter your name.';
}
elseif(empty($phone_number))
{
$error = 'You must enter a phone number.';
}
elseif(empty($email_address))
{
$error = 'You must enter your email address.';
}
elseif(empty($start_date))
{
$error = 'You must enter a start date.';
}
elseif(empty($end_date))
{
$error = 'You must enter an end date.';
}
elseif(empty($number_of_people))
{
$error = 'You must enter the number of people.';
}
// 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 message was entered
if(isset($error))
{
header('Location: index.php?e=' . urlencode($error));
exit;
}
$email_content .= "Email Address: $email_address\nName: $your_name\Phone Number: $phone_number\nStart Date: $start_date\End Date: $end_date\nNumber of People: $number_of_people\n";
// send the email
mail("g@gmail.com", "Booking form entry", $email_content);
mail("m@gmail.com", "Booking form entry", $email_content);
// send the user back to the form
header('Location: index.php?s=' . urlencode('Thank you for your booking enquiry. You will be contacted shortly.'));
exit;
?>