在表单顶部显示错误消息

时间:2011-01-13 16:07:45

标签: php

我正在尝试获取以下错误,以便在某些人按下提交按钮并且未填写所需字段时显示。

1 个答案:

答案 0 :(得分:0)

<?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 ..