【发布时间】:2017-03-24 08:40:09
【问题描述】:
我正在处理一个表单,除了自定义验证码之外,一切似乎都运行良好。无论我输入错误的答案还是实际正确的答案,表单都不会提交到数据库,并使用我的“请重试”错误消息重新加载表单...我希望该字段的验证遵循相同的格式和其他人一样,但不知道这是否可能(这是验证部分中的 $captchaValid )。这是我的表单中的 php 代码:
<?php
//captcha
session_start();
$digit1 = mt_rand(1, 10);
$digit2 = mt_rand(1, 10);
$math ="$digit1 + $digit2";
$_SESSION['answer'] = $digit1 + $digit2;
//error reporting
error_reporting(E_ALL);
ini_set('display_errors','1');
//include validation file here!!
include_once('validationresult.php');
//results
$firstnameValid = new ValidationResult("", "", "", true);
$lastnameValid = new ValidationResult("", "", "", true);
$emailValid = new ValidationResult("", "", "", true);
$captchaValid = new ValidationResult("", "", "", true);
//validation
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$firstnameValid = ValidationResult::checkParameter("firstname", '/^[a-zA-Z ]*$/', 'Only Letters and Spaces Allowed');
$lastnameValid = ValidationResult::checkParameter("lastname", '/^[a-zA-Z ]*$/', 'Only Letters and Spaces Allowed');
$emailValid = ValidationResult::checkParameter("email", '/(.+)@([^\.].*)\.([a-z]{2,})/', 'Please enter valid e-mail');
$captchaValid = ValidationResult::checkParameter("captcha", $_SESSION['answer'] == $_POST['answer'], 'Please try again');
//redirection
if ($firstnameValid->isValid() && $lastnameValid->isValid() && $emailValid->isValid() && $captchaValid->isValid() ) {
require "connectiontest.php";
header('Location: thankyou.html');
exit;
}
}
?>
以下是我单独的验证文件:
<?php
class ValidationResult
{
private $value;
private $cssClassName;
private $errorMessage;
private $isValid = true;
function __construct($cssClassName, $value, $errorMessage, $isValid)
{
$this->cssClassName = $cssClassName;
$this->value = $value;
$this->errorMessage = $errorMessage;
$this->isValid = $isValid;
}
public function getCssClassName() { return $this->cssClassName;}
public function getValue() { return $this->value;}
public function getErrorMessage() { return $this->errorMessage;}
public function isValid() { return $this->isValid;}
static public function checkParameter($queryName, $pattern, $errMsg) {
$error = "";
$errClass = "";
$value = "";
$isValid = "true";
if (empty($_POST[$queryName])) {
$error = $errMsg;
$errClass = "error";
$isValid = false;
}
else {
$value = $_POST[$queryName];
if ( ! preg_match($pattern, $value) ) {
$error = $errMsg;
$errClass = "error";
$isValid = false;
}
}
return new ValidationResult($errClass, $value, $error, $isValid);
}
}
?>
我试过改变这部分:
$captchaValid = ValidationResult::checkParameter("captcha", $_SESSION['answer'] == $_POST['answer'], 'Please try again');
几种不同的方法来看看我是否可以让它工作,但正如我所说,我什至不知道这是否可能(因为它不是真正的正则表达式?)。我尝试删除 "==$_POST..." 部分,将其括在单引号和双引号中,等等,要么得到错误,要么得到与以前相同的结果。如果无法实现我想要的,我感谢有关替代解决方案的建议...
一些注意事项:我是非常 php 新手,对任何事情都不太了解,所以请记住这一点。 另外,我还不知道javascript(在这种情况下并不重要)。 HTML、CSS 和 PHP 是关于我知识的广度。这是为了学校作业。
编辑 这是完整的代码,其中包含包含表单的 html。根据之前的建议进行了一些修改:
<?php
//captcha
if (!isset($_SESSION['answer'])) {
$digit1 = mt_rand(1, 10);
$digit2 = mt_rand(1, 10);
$math ="$digit1 + $digit2";
$_SESSION['answer'] = $digit1 + $digit2;
}
//error reporting
error_reporting(E_ALL);
ini_set('display_errors','1');
//include validation file here!!
include_once('validationresult.php');
//results
$firstnameValid = new ValidationResult("", "", "", true);
$lastnameValid = new ValidationResult("", "", "", true);
$emailValid = new ValidationResult("", "", "", true);
$captchaValid = new ValidationResult("", "", "", true);
//validation
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$firstnameValid = ValidationResult::checkParameter("firstname", '/^[a-zA-Z ]*$/', 'Only Letters and Spaces Allowed');
$lastnameValid = ValidationResult::checkParameter("lastname", '/^[a-zA-Z ]*$/', 'Only Letters and Spaces Allowed');
$emailValid = ValidationResult::checkParameter("email", '/(.+)@([^\.].*)\.([a-z]{2,})/', 'Please enter valid e-mail');
$captchaValid = ValidationResult::checkParameter("captcha", "/" . $_POST['answer'] . "/" , 'Please try again');
//redirection
if ($firstnameValid->isValid() && $lastnameValid->isValid() && $emailValid->isValid() && $captchaValid->isValid() ) {
require "connectiontest.php";
header('Location: thankyou.html');
exit;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Registration</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>" >
<?php echo $firstnameValid->getCssClassName(); ?>
<label for="firstname">First name:</label> <input type="text" name="firstname" value="<?php echo $firstnameValid->getValue(); ?>" required> <br>
<span class="help-inline" id="errorFirstname"> <?php echo $firstnameValid->getErrorMessage(); ?> </span> <br>
<?php echo $lastnameValid->getCssClassName(); ?>
<label for="lastname">Last name:</label> <input type="text" name="lastname" value="<?php echo $lastnameValid->getValue(); ?>" required> <br>
<span class="help-inline" id="errorLastname"> <?php echo $lastnameValid->getErrorMessage(); ?> </span> <br>
<?php echo $emailValid->getCssClassName(); ?>
<label for="email">E-mail:</label> <input type="email" name="email" value="<?php echo $emailValid->getValue(); ?>" required> <br>
<span class="help-inline" id="errorEmail"> <?php echo $emailValid->getErrorMessage(); ?> </span> <br>
<label for="optin">Opt In??</label><!--add legal-ese later--> <input type="checkbox" name="optin" required> <br>
<?php echo $captchaValid->getCssClassName(); ?>
<label for="captcha">captcha</label> <?php echo $math; ?> = <input type="text" name="captcha" value="<?php echo $captchaValid->getValue(); ?>" required><br>
<span class="help-inline" id="errorCaptcha"> <?php echo $captchaValid->getErrorMessage(); ?> </span> <br>
<input type="submit" value="Submit">
</form>
</body>
</html>
【问题讨论】:
标签: php forms validation