我很
抱歉新手问题,但我真的不知道该怎么做。所以首先我有在模态窗口中的联系表格。联系表单的服务器端(PHP 端)位于另一个文件contact.php
中。在此文件中,我对字段进行了一些验证。我想要的是当有空字段或错误的输入在表单上显示错误消息时。现在正在加载联系人.php错误就在那里。这是要从contact.php
进行验证的部分
function died($error) {
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['firstname']) ||
!isset($_POST['lastname']) ||
!isset($_POST['email']) ||
!isset($_POST['phone']) ||
!isset($_POST['description'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$firstname = $_POST['firstname']; // required
$lastname = $_POST['lastname']; // required
$email = $_POST['email']; // required
$phone = $_POST['phone']; // not required
$description = $_POST['description']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+'.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$error_message .= 'The email address is not valid!<br />';
}
$string_exp = "/^[a-zA-Z'p{Cyrillic}0-9's'-]+$/u";
if(!preg_match($string_exp,$firstname)) {
$error_message .= 'The name is not valid.<br />';
}
if(!preg_match($string_exp,$lastname)) {
$error_message .= 'The last name is not valid.<br />';
}
if(strlen($description) < 2) {
$error_message .= 'The field for description is not valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
...
和下面的 html 表单
<div class="send-icon" data-toggle="modal" data-target="#contact-form">
<i class="fa fa-paper-plane"></i>
</div>
<p class="light-text">Contact Us</p>
<!-- Contact Form Modal -->
<div class="modal fade contact-form" id="contact-form" tabindex="-1" role="dialog" aria-labelledby="contact-form" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<div class="modal-body">
<!-- ***** Contact form ***** -->
<form class="form" name="contactform" action="contact/contact.php" method="post">
<div class="row">
<div class="form-group col-md-6">
<input type="text" class="form-control" id="firstname" name="firstname" placeholder="firstname">
</div>
<div class="form-group col-md-6">
<input type="text" class="form-control" id="lastname" name="lastname" placeholder="lastname">
</div>
<div class="form-group col-md-6">
<input type="email" class="form-control" id="email" name="email" placeholder="Email" >
</div>
<div class="form-group col-md-6">
<input type="text" class="form-control" id="phone" name="phone" placeholder="Phone" >
</div>
<div class="form-group col-md-12 mab-none">
<textarea rows="6" class="form-control" id="description" name="description" placeholder="description" ></textarea>
</div>
<div class="form-group col-md-12">
<button class="button bold-text main-bg" type="submit" ><i class="fa fa-paper-plane"></i></button>
</div>
</div>
</form>
所以再次原谅我的问题,如果对你们来说"太新手",但我真的需要一些帮助。
假设您的代码与本教程完全相同(如您在注释中所说):
您拥有的:
所以目前你的表格在index.php
:
<!-- HTML stuff before -->
<form class="form" name="contactform" action="contact.php" method="post">
<!-- Input tags -->
</form>
<!-- HTML stuff after -->
表格按contact.php
处理:
if(isset($_POST['email'])) {
function died($error) {
echo 'You have an error blahblah.';
echo $error;
die();
}
/*
Tests to validate the values
Puts some strings in $error_message if not valide
*/
if (strlen($error_message) > 0) {
died($error_message);
// If there is an error, call the function that displays the message and stops everything
}
// Send email
}
我的建议:
最好是将HTML和PHP放在同一个页面中(就好像它在同一个文件中一样)。在index.php
开头使用 include():
<?php
include('contact.php');
?>
<!-- HTML stuff before -->
<?php
if (strlen($error_message) > 0) {
// Now we can display our error message here, with the HTML.
echo '<div class="error">There was an error: '.$error_message.'</div>';
}
?>
<form class="form" name="contactform" action="index.php" method="post">
<!-- See the "action" value has changed, as we won't go to a new "contact" page but stay here -->
</form>
<!-- HTML stuff after -->
然后contact.php
文件必须更新一点。
if(isset($_POST['email'])) {
// Notice here that the code won't be executed if the form has not been submitted: so it is not a problem to include the code before the HTML.
// We don't used the died() function anymore: delete it.
/*
That does not change:
Tests to validate the values
Puts some strings in $error_message if not valide
*/
if (strlen($error_message) == 0) {
// Send the mail only if there is no error.
// Send email
}
}
当然,这不是完美的联系页面(例如,我不会将错误存储在简单的$error_message
字符串中),但这是从PHP和HTML开始并了解其工作原理的良好开端。我鼓励您在了解该过程后更新和调整此代码。
*编辑:我看到了你对模态的评论。重新加载页面时,此代码不会打开模式,您仍然需要单击按钮.
如果strlen($error_message) > 0
,你必须强制模态的CSS样式显示它(style="display:block"
需要的地方).
或者用JS打开它(我假设你有一个函数.modal()
或.open()
某处强制它打开。(不是我最喜欢的解决方案,因为加载速度有点慢,用户会在打开之前看到没有模态的页面。