在if语句中,您需要这样做:
if (empty($_POST['name'])) { //Or replace $_POST['name'] with $firstname echo '<p style="color: red; text-align: center">Your first name is required</p>'; } else { echo "wassaup"; }
首先,您可以创建错误数组,您将把所有错误放在哪里
$error = array();
然后当你检查多个字段时
if (empty($_POST['firstname'])) $error[] = "First name can't be empty"; if (empty($_POST['lastname'])) $error[] = "Last name can't be empty"; // and so on
执行完所有make语句后,检查错误数组是否为空,如果没有显示错误
if (empty($error)) { // do something } else { // error exists you want to display all errors foreach ($error as $value) { echo '<ul><li>'.$value.'</li></ul>'; } }
你的语法错了 if (empty($_POST[$firstname])) 。你应该使用''单一的paranthesis in firstname 并删除此符号$。
if (empty($_POST[$firstname]))
firstname
更改代码
if (!isset($_POST['firstname'] || empty($_POST['firstname'])) { echo '<p style="color: red; text-align: center">Your first name is required</p>'; } else { echo "wassaup"; }
改变这一点 if (empty($_POST[$name])) { 至 if (empty($_POST['firstname'])) { 并再次检查。你的语法错了,这就是为什么它不起作用。
if (empty($_POST[$name])) {
if (empty($_POST['firstname'])) {