[英]Display error message at top of form
我試圖讓以下錯誤顯示,當有人按下提交按鈕並且未填寫必填字段時,就會顯示此錯誤。
<?php
require_once("includes/database.php");
require_once("includes/functions.php");
if(isset($_POST['full_name'])) {
$required = array('full_name','user_name','email','pwd','pwd2');
$missing = array();
$validation = array(
'full_name' => 'Please provide your full name',
'user_name' => 'Please provide your username',
'email' => 'Please provide your valid email address',
'pwd' => 'Please provide your password',
'pwd2' => 'Please confirm your password',
'userdup' => 'Username already registered',
'emaildup' => 'Email address already registered',
'mismatch' => 'Passwords do not match'
);
//Sanitise and clean function
$full_name = escape($_POST['full_name']);
$user_name = escape($_POST['user_name']);
$email = escape($_POST['email']);
$pwd = escape($_POST['pwd']);
$pwd2 = escape($_POST['pwd2']);
foreach($_POST as $key => $value) {
$value = trim($value);
if(empty($value) && in_array($key,$required)) {
array_push($missing,$key);
} else {
${$key} = escape($value); //Why exacly do you need to have extra stuff?
}
}
//iterate over the missing elements, and display them
foreach ($missing as $m){
echo echo "<strong>Error: </strong>: " . $validation[$m] . "<br>";
}
//You need to do some more stuffs, like query the DB to check if the username is a duplicate or not.
}
//...rest of your code ..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.