试图在PHP5.2中创建一个通用函数,错误消息未显示
您好,我正在尝试在PHP5.2中创建一个通用函数,但我无法获得要显示的错误消息。我不确定如何将变量传递给validate函数。如何创建等于所有输入字段的输入变量?我是否需要将所有输入放入试图在PHP5.2中创建一个通用函数,错误消息未显示,php,Php,您好,我正在尝试在PHP5.2中创建一个通用函数,但我无法获得要显示的错误消息。我不确定如何将变量传递给validate函数。如何创建等于所有输入字段的输入变量?我是否需要将所有输入放入数组?对于经验丰富的php程序员来说,这可能是一个简单的问题,但对我来说,我仍然对php的每一个方面都很熟悉。我在下面包含了我的代码。谢谢: <?php $debug=1; $output_form= true; //$valid_ = 0; //$e
数组
?对于经验丰富的php程序员来说,这可能是一个简单的问题,但对我来说,我仍然对php的每一个方面都很熟悉。我在下面包含了我的代码。谢谢:
<?php
$debug=1;
$output_form= true;
//$valid_ = 0;
//$error_text= "";
function validateFormInput($input, $patterns, &$errors)
{
$valid_ = false;
$new = false;
foreach($input as $key => $value) {
if(!preg_match($patterns[$key], $value, $match)) {
$new[$key] = $errors[$key];
//$output_form = false;
}
// Remove errors if validation is made
else
unset($errors[$key]);
$valid_= true;
//$output_form = true;
}
//$output_form = false;
return $new;
}
//initialization
$inputs = array(
"fname" => '',
"lname" => '',
"phone" => '',
"city" => '',
"state" => '',
);
$patterns = array(
"fname" => '/^[a-zA-Z]{2,15}$/',
"lname" => '/^[a-zA-Z]{2,15}$/',
"phone" => '/^(\(\d{3}\))(\d{3}\-)(\d{4})$/',
"city" => '/^[a-zA-Z]{3,20}$/',
"state" => '/^[a-zA-Z]{2}$/',
);
$errors = array(
"fname" => "Please enter your FIRST name between 2 and 15 letters.",
"lname" => "Please enter your LAST name between 2 and 15 letters.",
"phone" => "Please enter your phone number in (###)###-### format.",
"city" => "Please enter your city name between 3 and 20 letters.",
"state" => "Please enter your state's abbreviation (Example: CA).",
);
if(isset($_POST['submit'])) {
if ($debug) {
echo "<pre>";
print_r($_POST);
echo "</pre>";
//echo "<pre>";
//print_r($_FILES);
//echo "</pre>";
//die("temp stop point");
} //debug only
$fname=trim($_POST['fname']);
$lname=trim($_POST['lname']);
$phone=trim($_POST['phone']);
$city=trim($_POST['city']);
$state=trim($_POST['state']);
$inputs = $_POST;
//$output_form = false;
// Just unset the submit and pass the $_POST via $inputs
unset($inputs['submit']);
//$output_form = false;
validateFormInput($inputs,$patterns,$errors);
//$output_form = false;
if($errors)
echo implode("<br />",$errors);
$mobile = preg_replace('/\D+/', '', $phone);
$output_form = false;
}
//if ($valid_) {$output_form =false;}
//$not_valid = validateFormInput($inputs,$patterns,$errors);
//if($not_valid)
//echo implode("<br />",$not_valid);
//$output_form = false;
//else { $output_form = false;}
?>
?>
<!DOCTYPE html>
<html>
<head>
<title>Lesson 7</title>
<meta charset="UTF-8">
<meta name="description" content="php">
<meta name="keywords" content="php">
<meta name="author" content="William Payne">
<link rel="stylesheet" type="text/css" href="xxxxxxxxxxxx">
</head>
<body>
<div class="formLayout">
<?php
//form
if ($output_form) {
?>
<form action="<?=htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Info</legend>
<?=$error_text ?>
<label>First Name:</label><input name="fname" type="text" value="<?=$fname; ?>"><br>
<label>Last Name:</label><input name="lname" type="text" value="<?=$lname; ?>"><br>
<label>Phone Number:</label><input name="phone" type="text" value="<?=$phone; ?>"><br>
<label>City:</label><input name="city" type="text" value="<?=$city; ?>"><br>
<label>State:</label><input name="state" type="text" value="<?=$state; ?>"><br>
</fieldset>
<input name="submit" id="submit" type="submit" value="submit"><br>
</form>
<?php
} else {
//replace_mobile();
//Processing for database inclusion, email cofirmation can also go here, thank you
//2nd way to Solve Not using regex preg_replace $p = $phone; <?="$p[1]$p[2]$p[3]$p[5]$p[6]$p[7]$p[9]$p[10]$p[11]$p[12]";//
?>
<p><strong><?="$welcome_msg"; ?></strong></p>
<p><strong><?="$lname".''.', '. "$fname";?></strong></p>
<p><strong><?="$phone".''.', '."$mobile";?></strong></p>
<p><strong><?="$city".''.', '."$state";?></strong></p>
<?php
} //end if/else form output
?>
</div>
</body>
</html>
在我的评论之后,我的意思是将变量作为数组传递,然后在其上循环,应用您的模式,并通过引用传递$errors
:
// Errors array.
$errArray = [];
//function
function validateFormInput($input, $fieldName, $isRequired, $min, $max, $patterns, $example = '') {
// Trimming the input to get rid of empty spaces on the sides
$input = trim($input);
// If the provided value of required == 'yes', and variable is -
// empty add an error to the errors array.
if($isRequired == 'yes' && empty($input)){
$errArray[] = $fieldName. ' is required';
}else{
// If the input is less than min or more than max -
//compose the error and add it to the errors array.
if(strlen($input) < $min || strlen($input) > $max ){
$errArray[] = 'Please enter your ' .$fieldName.' between ' .$min. ' and ' .$max. ' letters.<br>';
}
// Checking against the given pattern.the array.
if(!preg_match($patterns, $input)){
//if the given example is not empty, add it to the error message.
$example = ($example) ? ', (Example: ' .$example. ')' : '';
$errArray[] = 'Wrong format of ' .$fieldName . $example;
}
}
}
";
$data=ob_get_contents();
ob_end_clean();
返回$data;
}
如果(isset($_POST['submit'])){
如果(ERRMODE){
echo printPre($\u POST);
$patterns = array(
"fname" => '/^[a-zA-Z]{2,15}$/',
"lname" => '/^[a-zA-Z]{2,15}$/',
"phone" => '/^(\(\d{3}\))(\d{3}\-)(\d{4})$/',
"city" => '/^[a-zA-Z]{3,20}$/',
"state" => '/^[a-zA-Z]{2}$/',
);
$errors = array(
"fname" => "Please enter your FIRST name between 2 and 15 letters.<br>",
"lname" => "Please enter your LAST name between 2 and 15 letters.<br>",
"phone" => "Please enter your phone number in (###)###-### format.<br>",
"city" => "Please enter your city name between 3 and 20 letters.<br>",
"state" => "Please enter your state's abbreviation (Example: CA).<br>",
);
// Pass by ref--------------------------------vv
function validateFormInput($input, $patterns, &$errors)
{
$new = false;
foreach($input as $key => $value) {
if(preg_match($patterns[$key], $value, $match))
unset($errors[$key]);
}
return (empty($errors));
}
$valid = false;
if(isset($_POST['submit'])) {
$inputs = $_POST;
unset($inputs['submit']);
$valid = validateFormInput($inputs,$patterns,$errors);
if(!empty($errors))
echo implode("<br />",$errors);
}
if(!$valid) {
?>
<form method="post">
<input type="text" name="fname" value="Name" />
<input type="text" name="lname" value="LastName" />
<input type="text" name="phone" value="123-123-1233" />
<input type="text" name="city" value="Reno" />
<input type="text" name="state" value="NsrV" />
<input type="submit" name="submit" value="submit" />
</form>
<?php }
echo printPre($\u文件);
$patterns = array(
"fname" => '/^[a-zA-Z]{2,15}$/',
"lname" => '/^[a-zA-Z]{2,15}$/',
"phone" => '/^(\(\d{3}\))(\d{3}\-)(\d{4})$/',
"city" => '/^[a-zA-Z]{3,20}$/',
"state" => '/^[a-zA-Z]{2}$/',
);
$errors = array(
"fname" => "Please enter your FIRST name between 2 and 15 letters.<br>",
"lname" => "Please enter your LAST name between 2 and 15 letters.<br>",
"phone" => "Please enter your phone number in (###)###-### format.<br>",
"city" => "Please enter your city name between 3 and 20 letters.<br>",
"state" => "Please enter your state's abbreviation (Example: CA).<br>",
);
// Pass by ref--------------------------------vv
function validateFormInput($input, $patterns, &$errors)
{
$new = false;
foreach($input as $key => $value) {
if(preg_match($patterns[$key], $value, $match))
unset($errors[$key]);
}
return (empty($errors));
}
$valid = false;
if(isset($_POST['submit'])) {
$inputs = $_POST;
unset($inputs['submit']);
$valid = validateFormInput($inputs,$patterns,$errors);
if(!empty($errors))
echo implode("<br />",$errors);
}
if(!$valid) {
?>
<form method="post">
<input type="text" name="fname" value="Name" />
<input type="text" name="lname" value="LastName" />
<input type="text" name="phone" value="123-123-1233" />
<input type="text" name="city" value="Reno" />
<input type="text" name="state" value="NsrV" />
<input type="submit" name="submit" value="submit" />
</form>
<?php }
}
$errors=validateFormInput($\u POST);
$formValid=(空($errors));
$fname=htmlspecialchars($_POST['fname']);
$lname=htmlspecialchars($_POST['lname']);
$phone=htmlspecialchars($_POST['phone']);
$city=htmlspecialchars($_POST['city']);
$state=htmlspecialchars($_POST['state']);
$mobile=preg_replace(“/\D+/”,“$phone”);
}
?>
第七课
你可以这样做:
PHP:
因此,上述参数为:
- $foo=
$input
,输入值
- $fieldName=
Foo Name
,错误消息中的输入名称
- $isRequired=
是
,检查是否需要它
- $min=
5
,允许的最小字符数
- $max=
20
,允许的最大字符数
- $pattern=
'/^[a-zA-Z]$/'
,要检查的模式
- $example=
”
,我没有提供示例,但这里可以是(示例:CA)
或(#####-###格式
最后,我们检查$errArray
是否为空,然后没有发现错误,否则我们将遍历errors数组并回显所有捕获的错误。您可以将变量作为数组传递,然后应用您的模式对其进行循环。因此,基本上有3个数组进入函数。您可以顺便存储错误,然后返回因此我将创建5个diff函数调用validateFormInput($foo,'foo name','yes',5,20',/^[a-zA-Z]$/'));然后只需更改vars,regex,yes,5,20以满足其他4个输入要求?抱歉,不确定您的确切意思。您希望它做什么?我修改了上面的代码,通过您的更改,我只需要表单显示错误出现的时间,而不显示有效的时间,我尝试添加了一些逻辑,即使有错误,它也会执行!您将使用相同的if($errors)
。因此,if($errors)echo'form here';
您不会在函数中执行此操作。如果有错误,函数只会返回数组()
(true),如果没有错误,则返回false
。我尝试使用此逻辑-if($errors)echo$output\u form=true;echo内爆(
,$errors)}else{echo$output\u form=true;捕获所有错误后仍返回表单。我的html表单上方有一个if$output\u form变量,然后在数据显示的表单下方有一个else。这样,表单在满足所有条件时不会显示,而只在错误出现时显示。感谢您的帮助!
$patterns = array(
"fname" => '/^[a-zA-Z]{2,15}$/',
"lname" => '/^[a-zA-Z]{2,15}$/',
"phone" => '/^(\(\d{3}\))(\d{3}\-)(\d{4})$/',
"city" => '/^[a-zA-Z]{3,20}$/',
"state" => '/^[a-zA-Z]{2}$/',
);
$errors = array(
"fname" => "Please enter your FIRST name between 2 and 15 letters.<br>",
"lname" => "Please enter your LAST name between 2 and 15 letters.<br>",
"phone" => "Please enter your phone number in (###)###-### format.<br>",
"city" => "Please enter your city name between 3 and 20 letters.<br>",
"state" => "Please enter your state's abbreviation (Example: CA).<br>",
);
// Pass by ref--------------------------------vv
function validateFormInput($input, $patterns, &$errors)
{
$new = false;
foreach($input as $key => $value) {
if(preg_match($patterns[$key], $value, $match))
unset($errors[$key]);
}
return (empty($errors));
}
$valid = false;
if(isset($_POST['submit'])) {
$inputs = $_POST;
unset($inputs['submit']);
$valid = validateFormInput($inputs,$patterns,$errors);
if(!empty($errors))
echo implode("<br />",$errors);
}
if(!$valid) {
?>
<form method="post">
<input type="text" name="fname" value="Name" />
<input type="text" name="lname" value="LastName" />
<input type="text" name="phone" value="123-123-1233" />
<input type="text" name="city" value="Reno" />
<input type="text" name="state" value="NsrV" />
<input type="submit" name="submit" value="submit" />
</form>
<?php }