Required Fields

I have been trying for several days to get some fields to be required. For example, I want to get the descendants first name, middle name, and surname, to be required. I can’t seem to get it to work right. I could use some help as to what I am doing wrong. I have tried so many things I can’t even remember them all. Here is my current code.

[php]<?php

$error = “”;
$descendant_first_name = $descendant_middle_name = $descendant_surname = “”;

/* Check to see if the html was sent via $_POST /
if ($_SERVER[“REQUEST_METHOD”] == “POST”){
/
If true we continue on with the script /
/
Subject and Email Variables */

$emailSubject = '2017 Windemuth Family Reunion Registration Form';

/* Gathering Data Variables */
/*compare the email address */
if($_POST['email'] != $_POST['confirm']) {
    #set error...
    $error .= 'The email addresses do not match, please correct and submit again<br>';
}

if (empty($_POST["descendant_first_name"])) {
   $error .= "Your first name is required<br>";
   $Descendant_First_Name = "";
} else {
   $Descendant_First_Name = test_input($_POST["descendant_first_name"]);
}

if (empty($_POST["descendant_middle_name"])) {
   $error .= "Your middle name is required<br>";
   $Descendant_Middle_Name = "";
} else {
   $Descendant_Middle_Name = test_input($_POST["descendant_middle_name"]);
}

if (empty($_POST["descendant_surname"])) {
    $error .= "Your surname is required<br>";
    $Descendant_Surname = "";
} else {
    $Descendant_Surname = test_input($_POST["descendant_surname"]);
}

$S_SO_First_Name = $_POST['s_so_first_name'];
$S_SO_Middle_Name = $_POST['s_so_middle_name'];
$S_SO_Last_Name = $_POST['s_so_last_name'];
$Child1_First_Name = $_POST['child1_first_name'];
$Child1_Middle_Name = $_POST['child1_middle_name'];
$Child1_Last_Name = $_POST['child1_last_name'];
$Child2_First_Name = $_POST['child2_first_name'];
$Child2_Middle_Name = $_POST['child2_middle_name'];
$Child2_Last_Name = $_POST['child2_last_name'];
$Child3_First_Name = $_POST['child3_first_name'];
$Child3_Middle_Name = $_POST['child3_middle_name'];
$Child3_Last_Name = $_POST['child3_last_name'];
$Child4_First_Name = $_POST['child4_first_name'];
$Child4_Middle_Name = $_POST['child4_middle_name'];
$Child4_Last_Name = $_POST['child4_last_name'];
$Street = $_POST['street'];
$City = $_POST['city'];
$StateProvince = $_POST['stateprovince'];
$PostalCode = $_POST['postalcode'];
$Phone = $_POST['phone'];
$Email = $_POST['email'];
$Confirm = $_POST['confirm'];
$Adults = $_POST['adults'];
$Children = $_POST['children'];
$RegistrationFee = $_POST['registrationfee'];
$Comments = $_POST['comments'];

$webMaster = $Descendant.'<[email protected]>';

$immigrant="";
foreach($_POST["immigrant"] as $option){
   $immigrant .= $option;
}

$body = <<<EOD
<br><hr><br>
Descendant_First_Name: $Descendant_First_Name <br>
Descendant_Middle_Name: $Descendant_Middle_Name <br>
Descendant_Surname: $Descendant_Surname <br>
S_SO_First_Name: $S_SO_First_Name <br>
S_SO_Middle_Name: $S_SO_Middle_Name <br>
S_SO_Last_Name: $S_SO_Last_Name <br>
Child1_First_Name: $Child1_First_Name <br>
Child1_Middle_Name: $Child1_Middle_Name <br>
Child1_Last_Name: $Child1_Last_Name <br>
Child2_First_Name: $Child2_First_Name <br>
Child2_Middle_Name: $Child2_Middle_Name <br>
Child2_Last_Name: $Child2_Last_Name <br>
Child3_First_Name: $Child3_First_Name <br>
Child3_Middle_Name: $Child3_Middle_Name <br>
Child3_Last_Name: $Child3_Last_Name <br>
Child4_First_Name: $Child4_First_Name <br>
Child4_Middle_Name: $Child4_Middle_Name <br>
Child4_Last_Name: $Child4_Last_Name <br>
Street: $Street <br>
City: $City <br>
StateProvince: $StateProvince <br>
PostalCode: $PostalCode <br>
Phone: $Phone <br>
Email: $Email <br>
Confirm: $Confirm <br>
Descended From: $immigrant <br>
Adults: $Adults <br>
Children: $Children <br>
RegistrationFee: $RegistrationFee <br>
Comments: $Comments <br>
EOD;

$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";

/* if we got any errors echo out the error otherwise send the mail */
if($error != "") {
   echo $error;
}else{
   $success = mail ($webMaster, $emailSubject, $body, $headers);
   echo "Your Registration has been submitted successfully. We will contact you by email and confirm your Registration within 48 hours.";
}

}else{
/* If $_POST check is false give this message */

echo "Form Submission error. <a href='javascript:history.back(1);'>Back</a>";

}

?> [/php]

Why don’t you make the fields required in your html form then in your php you only have to process them?

It is as simple as

 <form action="/action_page.php">
  Username: <input type="text" name="usrname" required>
  <input type="submit">
</form> 

I have found some mobile devices (not all) that the required field doesn’t work properly if this is going to be a responsive website in the past.

What I do is have a bunch of validation functions, here’s a portion of registration script that I was writing.

[php]/*

  • Begin of Validation.
    */

function checkContent($data) {
/* This makes sure user just didn’t type spaces in an attempt to make the form valid /
foreach ($data as $key => $value) {
$data[$key] = isset($value) ? trim($value) : ‘’;
}
/
If there are empty field(s) then set the error array to
* false otherwise it should be true.
*/
if (in_array("", $data, true)) {
return FALSE;
} else {
return TRUE;
}
}

function checkPassword($password) {
/*
*
* Explaining !preg_match_all(’$\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])(?=\S*[\W])\S*$’, $password)
* $ = beginning of string
* \S* = any set of characters
* (?=\S{8,}) = of at least length 8
* (?=\S*[a-z]) = containing at least one lowercase letter
* (?=\S*[A-Z]) = and at least one one uppercase letter
* (?=\S*[\d]) = and at least one number
* (?=\S*[\W]) = and at least a special character (non-word character)
* $ = end of the string:
*
/
if (!\preg_match_all(’$\S
(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$’, $password)) {
return FALSE;
} else {
return TRUE;
}
}

function checkEmail($email) {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return FALSE;
} else {
return TRUE;
}
}

function passwordMatch($password, $verify_password) {
if ($password === $verify_password) {
return TRUE;
} else {
return FALSE;
}
}

function emailMatch($email, $verify_email) {
if ($email === $verify_email) {
return TRUE;
} else {
return FALSE;
}
}

function accountStatus($email, $pdo) {
$query = “SELECT 1 FROM users WHERE email = :email”;
$stmt = $pdo->prepare($query);
$stmt->bindParam(’:email’, $email);
$stmt->execute();
$row = $stmt->fetch();
if ($row) {
return FALSE;
} else {
return TRUE;
}
}

function validate(array $error) {
foreach ($error as $status) {
if (!$status) {
return $error;
}
}
return true;
}

/*

  • End of Validation
    */

/*

  • Save data to database table after validation is done.
    */
    function saveRegistration(array $data, $pdo) {
    $password = password_hash($data[‘password’], PASSWORD_DEFAULT);

    $query = ‘INSERT INTO users(username, email, password, dateCreated) VALUES ( :username, :email, :password, NOW())’;
    $stmt = $pdo->prepare($query);
    $result = $stmt->execute([’:username’ => $data[‘username’], ‘:email’ => $data[‘email’], ‘:password’ => $password]);
    if ($result) {
    return TRUE;
    } else {
    return FALSE;
    }
    }[/php]

and here’s the portion of the script that checks it:
[php]$data = []; // An array that we setup as $data:
$error = [];
/*

  • When user click on the submit button we grab the hidden input variable and the
  • reason we do that instead of the regular submit button is to ensure that we get the click.
  • For some older I.E. browsers don’t register the click on the submit button (or so I am told).
    */
    $submit = filter_input(INPUT_POST, ‘action’, FILTER_SANITIZE_FULL_SPECIAL_CHARS); // using htmlspecialchars sanitizes the variable:

if ($submit && $submit === ‘submit’) {
/*
* Grab all the user’s input responses and store them it the array called $data. We
* will shorting be validating all the input fields and then storing the values in a database table if everything
* passes validation.
*/
$data[‘username’] = htmlspecialchars($_POST[‘username’]);
$data[‘password’] = htmlspecialchars($_POST[‘password’]);
$data[‘password_verify’] = htmlspecialchars($_POST[‘verify’]);
$data[‘email’] = htmlspecialchars($_POST[‘email’]);
$data[‘email_verify’] = htmlspecialchars($_POST[‘verifyEmail’]);

/*
 * Validate user's input from registration form. Check to see if all fields have been entered, password is 
 * valid, email is valid, verify that both password and email address has been entered correct and make sure there
 * are no dupicate accounts being entered. 
 */
$error['empty'] = checkContent($data);
$error['password'] = checkPassword($data['password']);
$error['email'] = checkEmail($data['email']);
$error['passwordMatch'] = passwordMatch($data['password'], $data['password_verify']);
$error['emailMatch'] = emailMatch($data['email'], $data['email_verify']);
$error['account'] = accountStatus($data['email'], $pdo);

/*
 * Check to see if everything passes, if so save user's acount to database table users. Otherwise inform
 * user there was an error(s) when registering and please try again. 
 */
$result = validate($error);
if (!is_array($result)) {
    $info = saveRegistration($data, $pdo);
    unset($data);
} else {
    //echo "<pre>" . print_r($error, 1) . "</pre>\n";
}

}[/php]

and you can get the whole thing at : https://github.com/Strider64/php-registration-tutorial

maybe this will help or not. However, I think with a few modifications (and improvements of my code ;D ) it should be relatively easy to do.

I could probably do it this way, and it would no doubt work just fine. But I am trying to learn how to write the PHP code and every time I get something to work correctly, I learn from it. If I don’t do it this way and get it working, then I don’t really learn from it. I appreciate your help, it’s just that I want to try and get it to work with the PHP code. I think I am fairily close with the code I have so far, but I think I may have gotten something a little bit wrong. If I could just get it to work I would have a better understanding of it. I’ll keep trying and see if I can get it.

I’ll take a close look at this and see what I can come up with for ideas to fix mine. Thanks for the info. I will be gone for a week or ten days, so I won’t be working on it for awhile.

Sponsor our Newsletter | Privacy Policy | Terms of Service