我正在尝试使用jQuery验证插件验证带有php和客户端站点的contactform服务器。首先,当我在输入电子邮件地址后使用jQuery验证插件后,我在@之后输入了@和1个字符jQuery验证插件即使没有.com,.org等也会通过电子邮件地址。在那里演示网站你可以看到我的内容意思是:[http://jqueryvalidation.org/files/demo][1]。其次我试图用PHP验证服务器站点,但这不起作用,我无法找出它出错的地方。验证文件如下所示:
<?php
// We gaan de errors in een array bijhouden
$aErrors = array();
$aErrors ['name'] = '';
$aErrors ['email'] = '';
$aErrors ['message'] = '';
if ('POST' == $_SERVER['REQUEST_METHOD']) {
// Er zijn gegevens verstuurd naar deze pagina!
// Een naam bevat letters en spaties (minimaal 3)
if ( !isset($_POST['name']) or !preg_match( '~^[\w ]{2,}$~', $_POST['name'] ) ) {
$aErrors['name'] = 'Please fill in your name';
}
// Een email-adres is wat ingewikkelder
if ( !isset($_POST['email']) or !preg_match( '~^[a-z0-9][a- z0-9_.\-]*@([a-z0-9]+\.)*[a-z0-9][a-z0-9\-]+\.([a-z]{2,6})$~i', $_POST['email'] ) ) {
$aErrors['email'] = 'Please fill in your e-mail address';
}
// Een adres heeft letters, cijfers, spaties (minimaal 5)
if ( !isset($_POST['message']) or !preg_match( '~^[\w\d ]{5,}$~', $_POST['message'] ) ) {
$aErrors['message'] = 'What would you like to share?';
}
if ( count($aErrors) == 0 ) {
// We hebben alle gegevens
// Gegevens verwerken!
// Volgende pagina aub
header('Location: http://www.phpfreakz.nl/someotherpage.php');
exit();
}
}
?>
在index.php的顶部需要一次validation.php,例如:
<?php require_once 'php/validation.php'; ?>
<!doctype html>
<html lang="en" class="no-js">
<head>
<title>Alternate Fixed And Scroll Backgrounds</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
自己的联系表格如下:
<div class="contact">
<div class="formfield">
<form method="post" class="cmxform" id="commentForm" action="php/contact.php">
<div class="fieldset">
<input id="name" name="name" class="error_name" type:"text" placeholder="What is your name?" value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '' ?>" />
<div class="error_name"><?php print $aErrors['name'];?></div>
<input id="email" name="email" class="error_email" type="email" placeholder="email" value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : '' ?>" />
<div class="error_email"><?php print $aErrors['email'];?></div>
<textarea id="message" name="message" class="error_message" placeholder="message" value="<?php echo isset($_POST['message']) ? htmlspecialchars($_POST['message']) : '' ?>" /></textarea>
<div class="error_message"><?php print $aErrors['message'];?></div>
<input name="submit" type="submit" value="Versturen" />
</div>
</form>
</div>
</div>
我在PHP验证中做错了什么,它不起作用。当我删除:
<?php require_once 'php/validation.php'; ?>
我收到一个错误,所以它知道它在那里但是没有对它做任何事情。
这是我更改联系表格的方式。验证现在放在PHPmailer的前面。正确输入联系表格后,邮件将按原样发送。但是当表单输入不正确并且我点击提交时,将不会显示任何错误消息,我将转移到blanc contact.php页面。
contact.php的外观如下:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
require_once 'PHPMailerAutoload.php';
$aErrors = array();
$aErrors ['name'] = '';
$aErrors ['email'] = '';
$aErrors ['message'] = '';
if ('POST' == $_SERVER['REQUEST_METHOD']) {
// Er zijn gegevens verstuurd naar deze pagina!
// Een naam bevat letters en spaties (minimaal 3)
if ( !isset($_POST['name']) or !preg_match( '~^[\w ]{3,}$~', $_POST['name'] ) ) {
$aErrors['name'] = 'Please fill in your name';
}
// Een email-adres is wat ingewikkelder
if ( !isset($_POST['email']) or !preg_match( '~^[a-z0-9][a- z0-9_.\-]*@([a-z0-9]+\.)*[a-z0-9][a-z0-9\-]+\.([a-z]{2,6})$~i', $_POST['email'] ) ) {
$aErrors['email'] = 'Please fill in your e-mail address';
}
// Een adres heeft letters, cijfers, spaties (minimaal 5)
if ( !isset($_POST['message']) or !preg_match( '~^[\w\d ]{5,}$~', $_POST['message'] ) ) {
$aErrors['message'] = 'What would you like to share?';
}
if ( count($aErrors) == 0 ) {
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'xxxxxxxxxxxxx'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxxx'; // SMTP username
$mail->Password = 'xxxxxxxxxxxxxx'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->From = 'xxxxxxxxx';
$mail->FromName = $_POST['name'];
$mail->addAddress('x', 'x'); // Add a recipient
// Name is optional
$mail->addReplyTo($_POST['email']);
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Contact formulier';
$mail->Body = $_POST['message'];
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if($mail->send()) {
header('Location: localhost:8888/one_page_redirect kopie 3');
die();
} else {
$errors[] ='Sorry, could not send email. Try again later.';
}
}
} else {
$errors[] = 'Something went wrong.';
}
?>
联系表格如下:
<div class="contact">
<div class="formfield">
<form action="php/contact.php" method="post" class="cmxform">
<?php
if ( isset($aErrors) and count($aErrors) > 0 ) {
foreach ( $aErrors as $error ) {
print '<div>' . $error . '</div>';
}
}
?>
<div class="fieldset">
<input id="name" class="error_name" name="name" placeholder="What is your name?" value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name']) : '' ?>" />
<?php if (!empty($aErrors['name'])) { ?><div class="error_name"><?php print $aErrors['name'];?></div><?php } ?>
<input id="email" name="email" placeholder="email" value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : '' ?>" />
<?php if (!empty($aErrors['email'])) { ?><div class="error_email"><?php print $aErrors['email'];?></div><?php } ?>
<textarea id="message" class="error_message" name="message" placeholder="message" value="<?php echo isset($_POST['message']) ? htmlspecialchars($_POST['message']) : '' ?>" /></textarea>
<?php if (!empty($aErrors['newt_win_messagev(title, button_text, format, args)'])) { ?><div class="error_message"><?php print $aErrors['message'];?></div><?php } ?>
<input name="submit" type="submit" value="Versturen" />
</div>
</form>
</div>
</div>
提前完成。
答案 0 :(得分:0)
对于服务器端我使用:
if (!filter_var($value, FILTER_VALIDATE_EMAIL))
{
// Not Valid
}
对于客户端验证,请查看:Email validation using jQuery