我已经基于http://rosstanner.co.uk/2012/11/build-simple-contact-form-html-php-bootstrap/
创建了一个html / php表单它成功验证了表单并将发送邮件。但是,如果表单无效,则会将用户返回到index.php,但会丢失他们输入的表单值,这显然是一个糟糕的用户体验。
我尝试使用cookies来纠正这个问题,但我觉得这种做法很糟糕,而且无论如何它似乎都不适用于iphone。我有什么建议可以实现这个目标吗?
的index.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;
?>
答案 0 :(得分:0)
您应该尝试使用$ _SESSION而不是Cookie。 它就像
一样简单$_SESSION['key'] = $value;
但是当用户在关闭浏览器表单后再次回来时会被清除。 因此,请考虑将该数据存储在文件或数据库中。您可以通过IP识别用户。
答案 1 :(得分:0)
我建议您将数据保留在$_SESSION['name']=$value;
而不是$_COOKIE
为了防止提交错误的数据,您可以使用jQuery validate link并在提交表单之前检查用户输入。然后您可以注意到用户向每个字段插入有效信息。
例如:link
要阻止提交空的但必填的表单字段,您可以使用HTML属性required
例如
<input class="textfield" name="your_name" placeholder=
"Enter your name" value="<?php echo $return_form_vals['your_name'];?>" type="text" required="required"><br>
在填写所有必填字段之前,表单不允许用户提交