[英]PHP. Contact Form on error change color of text area without redirecting
I have contact form, which successfully send message to my email. 我有联系表,可以将信息成功发送到我的电子邮件中。 But when user enter invalid data (for example not entered email) user redirecting to
send_form_email.php
and errors are shown "Please enter email etc..." I need to make that If error occurred, error should be displayed in the same window without redirecting and input field should become "red" color (as I understand need to change css style, but how to do It correctly?). 但是,当用户输入无效数据(例如未输入电子邮件)时,用户重定向到
send_form_email.php
并显示错误“请输入电子邮件等...”。我需要确保如果发生错误,则应在同一窗口中显示错误而不显示错误。重定向和输入字段应变为“红色”颜色(据我了解,需要更改CSS样式,但如何正确进行操作?)。
Here is my contact form: 这是我的联系表格:
Here is how error looks like now: 这是现在错误的样子:
Here is how error should be: 错误应该是这样的:
This is part of my contact form, how "e-mail" field looks like: 这是我的联系表格的一部分,“电子邮件”字段如下所示:
<form name="contactform" class="form_row" method="post" action="send_form_email.php">
<table width="450px">
<tr>
<td valign="top">
<label for="email">El. paštas: *</label>
</td>
<td valign="top">
<input type="text" class="contact_input" name="email" maxlength="80" size="30">
</td>
</tr>
Here is contact_input style: 这是contact_input样式:
input.contact_input{
width:225px;
height:18px;
float:left;
border:1px #d1e0ee solid;
background-color:#fee9a1;
color: #000;
}
And here is part where redirecting after error occurred. 这是发生错误后重定向的部分。
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();
}
UPDATE 更新
This is my full send_form_email.php
这是我的完整
send_form_email.php
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "myMail@gmail.com";
$email_subject = "Message from myPage";
function died($error) {
session_start();
$_SESSION['error'] = $error;
header('Location: contact.php');
die();
}
if (!is_valid_email($_POST['email'])) {
$error['email'] = 'email should be entered';
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name'];
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Pranešimas apačioje.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Vardas: ".clean_string($name)."\n";
$email_message .= "El. paštas: ".clean_string($email_from)."\n";
$email_message .= "Tel. Nr.: ".clean_string($telephone)."\n";
$email_message .= "Pranešimas: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
You can add client side validation using jQuery and submit your form using Ajax 您可以使用jQuery添加客户端验证,并使用Ajax提交表单
Assuming you html form like this 假设您这样的html表单
<form id="myForm">
<table>
<tr>
<td colspan="2"><div id="result"></div></td>
</tr>
<tr>
<td valign="top"><label for="email">El. pa?tas: *</label></td>
<td valign="top">
<input id="fEmail" type="text" class="contact_input" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top"></td>
<td valign="top">
<input id="btnSend" name="Send" type="button" value="Send"/>
</td>
</tr>
</table>
</form>
and add the following css to your stylesheet 并在样式表中添加以下CSS
.input-error
{
background-color: #AA0000 !important;
}
and the following jQuery script 和以下jQuery脚本
<script type="text/javascript">
$('.error').hide();
$('#btnSend').click(function () {
$('.error').hide();
var email = $("input#fEmail").val();
if (email == "") {
$("input#fEmail").focus();
$("input#fEmail").addClass("input-error");
return false
}
$.ajax({
type: "POST",
url: "send_form_email.php",
data: $('#myForm').serialize(),
success: function (response) {
$('#result').html("<div class='success'>" + response + "</div>")
}, failed: function (response) {
$('#result').html("<div class='error'>" + response + "</div>");
}
});
return false
});
If you want use server side validation using PHP: 如果要使用PHP使用服务器端验证:
First of all you need to assign error values when checking posted values for example : 首先,您需要在检查过帐值时分配错误值,例如:
if (!is_valid_email($_POST['email'])) {
$error['email'] = 'email should be entered';
}
Then if there is any errors you should pass $error to form page. 然后,如果有任何错误,则应将$ error传递给表单页面。 For example using session :
例如使用session:
function died($error) {
session_start();
$_SESSION['error'] = $error;
header('Location: form_page.php');
die();
}
Then in your form page : 然后在您的表单页面中:
session_start();
if (isset($_SESSION['error']['email'])) {
echo '<input type="text" class="contact_input error" name="email" maxlength="80" size="30" value="' . $_SESSION['error']['email'] . '">';
} else {
// empty input
}
session_destroy();
css : css:
.error
{
background-color: red !important;
}
EDIT 编辑
send_form_email.php should look like this : send_form_email.php应该如下所示:
<?php
if (isset($_POST['email'])) {
$email_to = "myMail@gmail.com";
$email_subject = "Message from myPage";
if (!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
$error['form'] = 'We are sorry, but there appears to be a problem with the form you submitted.';
}
$name = $_POST['name'];
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if (!preg_match($email_exp, $email_from)) {
$error['email'] = 'The Email Address you entered does not appear to be valid.';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $name)) {
$error['name'] = 'The First Name you entered does not appear to be valid.';
}
if (strlen($comments) < 2) {
$error['comment'] = 'The Comments you entered do not appear to be valid.';
}
if (count($error) > 0) {
died($error);
}
$email_message = "Pranešimas apačioje.\n\n";
function clean_string($string)
{
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message .= "Vardas: " . clean_string($name) . "\n";
$email_message .= "El. paštas: " . clean_string($email_from) . "\n";
$email_message .= "Tel. Nr.: " . clean_string($telephone) . "\n";
$email_message .= "Pranešimas: " . clean_string($comments) . "\n";
// create email headers
$headers = 'From: ' . $email_from . "\r\n" .
'Reply-To: ' . $email_from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
}
function died($error) {
session_start();
$_SESSION['error'] = $error;
header('Location: contact.php');
die();
}
This is just part of HTML in contact.php from your question : 这只是来自您的问题contact.php中HTML的一部分:
<form name="contactform" class="form_row" method="post" action="send_form_email.php">
<?php
session_start();
if (isset($_SESSION['error']['form'])) {
echo '<span class="error">' . $_SESSION['error']['form'] . '</span>';
}
?>
<table width="450px">
<tr>
<td valign="top">
<label for="email">El. paštas: *</label>
</td>
<td valign="top">
<?php
if (isset($_SESSION['error']['email'])) {
echo '<input type="text" class="contact_input error" name="email" maxlength="80" size="30" value="' . $_SESSION['error']['email'] . '">';
} else {
echo '<input type="text" class="contact_input" name="email" maxlength="80" size="30">';
}
?>
</td>
</tr>
<?php
session_destroy(); // THIS SHOULD BE IN THE END OF THE FILE
?>
You should repeat html part to all input fields 您应该对所有输入字段重复html部分
Please take your time to understand all this. 请花些时间来了解所有这一切。 Sėkmės!
Sėkmės!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.