Jump to content

Recommended Posts

I am trying to write some code to validate if form fields are filled in, but with this code, it does not echo the error message, and if filled in does not add the user... maybe one of the pros can spot my error...

 

<?php
elseif (isset($_POST['login'])) {
$errors = array();
    $error_msg = '';
    $firstname = trim($_POST['fname']);
    $lastname = trim($_POST['lname']);
    $empid = trim($_POST['empid']);
$email = trim($_POST['email']);
    $username = trim($_POST['uname']);
    $password = md5($_POST['pass']);
    $office = trim($_POST['office']);
    $agency = $_SESSION['SESS_AGENCY'];
$level = trim($_POST['level']);

 if (empty($firstname)) { $errors[] = "First name is required."; }
    if (empty($lastname)) { $errors[] = "Last name is required."; }
    if (empty($empid)) { $errors[] = "Employee ID is required."; }
    if (empty($email)) { $errors[] = "Email is required."; }
    if (empty($username)) { $errors[] = "User name is required."; }
if (empty($password)) { $errors[] = "Password is required."; }
if (empty($office)) { $errors[] = "Office is required."; }
if (empty($level)) { $errors[] = "User level is required."; }

    //Create error message if needed
    if (count($errors)>0)
    {
        $error_msg = "The following errors occured. Please correct and resubmit the form:<br />\n";
        $error_msg .= "<ul><li>" . implode("</li>\n<li>", $errors) . "</li></ul>";
	echo $error_msg;

    }
    else
    {


    $usql = mysql_query("INSERT INTO members SET firstname = '$firstname', lastname = '$lastname', empid = '$empid', login = '$username', passwd = '$password', office = '$office', agency = '$agency', email = '$email', level = '$level'");
 echo mysql_error();                                                                                                                  
    mysql_free_result($usql);
    echo "<b>User Added Successfully!<br>";
    echo "<meta http-equiv=Refresh content=2;url=admin.php>";
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/151653-solved-form-validation-problem/
Share on other sites

Try this to carry out the validation part

 

if ( $firstname == '')
{
$errors[] = "First name is required.";
}
elseif ($lastname == '') 
{
$errors[] = "Last name is required.";
}
elseif ($empid == '') 
{
$errors[] = "Employee ID is required.";
}
elseif ($username == '') 
{
$errors[] = "Username is required.";
}
elseif ($password == '') 
{
$errors[] = "Password is required.";
}
elseif ($office == '') 
{
$errors[] = "Office is required.";
}  
elseif ($level == '') 
{
$errors[] = "User level is required.";
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.