在登录表单上测试错误消息时 Foreach 无效
Invalid Foreach when testing error messages on login form
我又回来了。我已经为表单制作了这个 php 代码-process.php;
<?php
//prevent access if they haven't submitted the form.
if (!isset($_POST['submit'])) {
die(header("Location: form.php"));
}
session_start();
$_SESSION['formAttempt'] = true;
if (isset($_SESSION['error'])){
unset($_SESSION['error']);
}
$required = array ("name", "email", "password1", "password2");
$_SESSION['error'] = array();
//Check required fields
foreach ($required as $requiredField) {
if(!isset($_POST[$requiredField]) || $_POST[$requiredField] == "") {
$_SESSION['error'] [] = $requiredField . "is required." ;
}
}
//Validating Text in name
if (!preg_match('/^[/w.]+$/',$_POST['name'])) {
$_SESSION['error'] [] = "Name must be letters and numbers only.";
}
//Validating Drop Down Selection
$validStates = array("Alabama","California", "Colorado", "Florida", "Illinois", "New York");
if (isset($_POST['state']) && $_POST['state'] != "") {
if(!in_array($_POST['state'], $validStates)) {
$_SESSION['error'] []="Please choose a valid state.";
}
}
//Validating an Email
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$_SESSION['error'] = "Invalid email address";
}
//Ensure that the Passwords Match
if ($_POST['password1'] != $_POST['password2']) {
$_SESSION['error'] [] = "Passwords do not match.";
}
//Final Disposition
if (isset($_SESSION['error']) && count ($_SESSION['error']) > 0) {
die(header("Location: form.php"));
} else {
unset($_SESSION['formAttempt']);
die(header("Location:success.php"));
}
?>
登录表单Div错误php代码;
<?php
if (isset($_SESSION['error']) && isset($_SESSION['formAttempt'])) {
unset ($_SESSION['formAttempt']);
print "Errors encountered <br />\n";
foreach ($_SESSION['error'] as $error) {
print $error . "<br />\n"; }
}
?>
但是,在实际的登录表单上测试时,我得到了这个错误:
Warning: Invalid argument supplied for foreach() in E:\XAMPP\htdocs\form.php on line 20
我是 PhP 的新手,所以我可能遗漏了一些东西,但我不太清楚...抱歉
$_SESSION['error'] = "Invalid email address";
这将使它成为字符串而不是先前初始化的数组,从而导致 foreach 出错。尝试:
$_SESSION['error'][] = "Invalid email address";
还有
$_SESSION['error'] [] = "Passwords do not match.";
$_SESSION['error'] 和 [] 之间不应有 space,不确定这是否会导致问题,有多个地方有额外的 space 在你的验证中。
我又回来了。我已经为表单制作了这个 php 代码-process.php;
<?php
//prevent access if they haven't submitted the form.
if (!isset($_POST['submit'])) {
die(header("Location: form.php"));
}
session_start();
$_SESSION['formAttempt'] = true;
if (isset($_SESSION['error'])){
unset($_SESSION['error']);
}
$required = array ("name", "email", "password1", "password2");
$_SESSION['error'] = array();
//Check required fields
foreach ($required as $requiredField) {
if(!isset($_POST[$requiredField]) || $_POST[$requiredField] == "") {
$_SESSION['error'] [] = $requiredField . "is required." ;
}
}
//Validating Text in name
if (!preg_match('/^[/w.]+$/',$_POST['name'])) {
$_SESSION['error'] [] = "Name must be letters and numbers only.";
}
//Validating Drop Down Selection
$validStates = array("Alabama","California", "Colorado", "Florida", "Illinois", "New York");
if (isset($_POST['state']) && $_POST['state'] != "") {
if(!in_array($_POST['state'], $validStates)) {
$_SESSION['error'] []="Please choose a valid state.";
}
}
//Validating an Email
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$_SESSION['error'] = "Invalid email address";
}
//Ensure that the Passwords Match
if ($_POST['password1'] != $_POST['password2']) {
$_SESSION['error'] [] = "Passwords do not match.";
}
//Final Disposition
if (isset($_SESSION['error']) && count ($_SESSION['error']) > 0) {
die(header("Location: form.php"));
} else {
unset($_SESSION['formAttempt']);
die(header("Location:success.php"));
}
?>
登录表单Div错误php代码;
<?php
if (isset($_SESSION['error']) && isset($_SESSION['formAttempt'])) {
unset ($_SESSION['formAttempt']);
print "Errors encountered <br />\n";
foreach ($_SESSION['error'] as $error) {
print $error . "<br />\n"; }
}
?>
但是,在实际的登录表单上测试时,我得到了这个错误:
Warning: Invalid argument supplied for foreach() in E:\XAMPP\htdocs\form.php on line 20
我是 PhP 的新手,所以我可能遗漏了一些东西,但我不太清楚...抱歉
$_SESSION['error'] = "Invalid email address";
这将使它成为字符串而不是先前初始化的数组,从而导致 foreach 出错。尝试:
$_SESSION['error'][] = "Invalid email address";
还有
$_SESSION['error'] [] = "Passwords do not match.";
$_SESSION['error'] 和 [] 之间不应有 space,不确定这是否会导致问题,有多个地方有额外的 space 在你的验证中。