Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1035177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:30:21+00:00 2026-05-16T14:30:21+00:00

Here is the problem. I have data validation and sanitation code in PHP and

  • 0

Here is the problem.
I have data validation and sanitation code in PHP and an HTML form.
When there is some field left empty on the form, the validation code executes and prints a message. I thought I designed the form the way all the filled out fields retain their input data at that point. However, it proves to be inconsistent. For example, first and last name fields become empty, whereas the phone and street fields still have the data entered by a user before the validation occurred.

Here is the HTML:

<p>
    <label for="firstname">FIRST NAME*:
    </label>
    <input type="text" name="firstname" id="firstname" value="<?php echo $firstname?>" />
</p>    

<p>
    <label for="lastname">LAST NAME*:
    </label>
    <input type="text" name="lastname" id="lastname" value="<?php echo $lastname?>" />
</p>    

<p>
    <label for="phone">TELEPHONE NUMBER*:
    </label>
    <input type="text" name="phone" id="phone" value="<?php echo $phone?>" />
</p>

    <p>
    <label for="street">STREET ADDRESS*:
    </label>
    <input type="text" name="street" id="street" value="<?php echo $street?>" />
</p>

Here is the validation script:

if ($_POST['firstname'] != "") {
        $_POST['firstname'] = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
        if ($_POST['firstname'] == "") {
            $errors .= 'Please enter a valid first name.<br/><br/>';
        }
    } else {
        $errors .= 'Please enter your first name.<br/>';
    }

    if ($_POST['lastname'] != "") {
        $_POST['lastname'] = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);
        if ($_POST['lastname'] == "") {
            $errors .= 'Please enter a valid last name.<br/><br/>';
        }
    } else {
        $errors .= 'Please enter your last name.<br/>';
    }

    if ($_POST['phone'] != "") {
        $phone = filter_var($_POST['phone'], FILTER_SANITIZE_NUMBER_INT);
        if ($_POST['phone'] == "") {
            $errors .= 'Please enter a valid phone number.<br/><br/>';  
        }
    } else {
        $errors .= 'Please enter your phone number.<br/>';
    }

    if ($_POST['street'] != "") {
        $street = filter_var($_POST['street'], FILTER_SANITIZE_STRING);
        if ($_POST['street'] == "") {
            $errors .= 'Please enter a valid street address.<br/><br/>';
        }
    } else {
        $errors .= 'Please enter your street address.<br/>';
    }

To me all the fields look the same.
What could cause the inconsistency?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:30:21+00:00Added an answer on May 16, 2026 at 2:30 pm

    You are setting up your $phone and $street variables, but not $firstname or $lastname. Instead you are are reassigning the $_POST variables:

    $_POST['lastname'] = filter_var($_POST['lastname'], FILTER_SANITIZE_STRING);

    filter_var returns either the filtered input or false if the filter fails, so you should also be checking that the result of your filter is not false, (instead of == "") in order to trigger your error.

    For instance:

    if ($_POST['firstname'] != "")
    {
      $firstname = filter_var($_POST['firstname'], FILTER_SANITIZE_STRING);
    
      if ($firstname === false)
      {
        $errors .= 'Please enter a valid first name.<br/><br/>';
      }
    }
    else
    {
      $errors .= 'Please enter your first name.<br/>';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the problem: I have a data-bound list of items, basically a way for
So here's the problem. I have data in a MySQL DB as text. The
Here is my problem I have arraycollection with the attribute Month with similar data
I have a problem here that requires to design a data structure that takes
I have encountered a problem of placing data into the Table View. Here is
Okay, here is my problem: I have a form that requires to have two
Simple problem here (I think). I have a form with a checkbox at the
I have jQuery validation on many forms. The problem is that on some of
Here's the use case: I have this long form with group of field that
Here is the problem: I have a simple form (textboxes, etc). The bindings are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.