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 8736805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:22:34+00:00 2026-06-13T10:22:34+00:00

I want to validate a form with php. Therefor I created a class benutzer

  • 0

I want to validate a form with php.

Therefor I created a class “benutzer” and a public function “benutzerEintragen” of this class to validate the form:

class benutzer
{
private $username = "";
private $mail = "";
private $mail_check = "";
private $passwort = "";
private $passwort_check = "";
public $error_blank = array();
public $error_notSelected = array();
public $error_notEqual = array();

    public function benutzerEintragen () {
    $textfields[0] = $this->username;
    $textfields[1] = $this->mail;
    $textfields[2] = $this->mail_check;
    $textfields[3] = $this->passwort;
    $textfields[4] = $this->passwort_check;
    foreach ($textfields as $string) {
      $result = checkFormular::emptyVariable($string);
      $error_blank[] = $result; 
    }

In the function “benutzerEintragen” i filled the variables “username,mail” and so on with the appropriate $_POST entries (not shown in the code above). The call

checkFormular::emptyVariable($string) 

just returns “TRUE” if the field is not set or empty otherwise FALSE.

Now when i try to create a new instance of this class, execute the function and get access to $error_blank[0] the array is empty!

 if (($_SERVER['REQUEST_METHOD'] == 'POST')){ 
 $BENUTZER = new benutzer();
 $BENUTZER->benutzerEintragen();
 echo $BENUTZER->error_blank[0];}

So the last line is leading to a “Notice: Undefined offset: 0”. It seems to be related to the array structure, because if i do

 echo $BENUTZER->mail;

I get any input I wrote in the form, which is correct. Also the foreach loop seems to do the right thing when i run the debugger in phpEd, but it seems like the array “error_blank” is erased after the function is executed.

Any help would be greatly appreciated. Thanks in advance.

  • 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-06-13T10:22:35+00:00Added an answer on June 13, 2026 at 10:22 am

    There is a scope problem here. You do have a class attribute with the name. Unlike in Java where using a local variable with the same name as a class variable automatically selects the class attribute this is not the case in PHP.

    Basically you are saving your output in a local variable which gets discarded once you leave the function. Change $error_blank[] = $result; to $this->error_blank[] = $result; and you should be fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to validate this form ignoring default values of input fields. I tried
I am trying to validate form input data using PHP's preg_match function. I am
Using this jQuery validation plugin. http://www.benjaminkeen.com/software/rsv/jquery/index.php I want to validate a checkbox group with
I want to validate a form using PHP. Below is the sample of my
I want to validate a form fields (text) input. It should only be a
Hi I want to validate a form input so it only takes the numbers
Im using asp.net webforms. c# I want to validate a form. In the codebehind
I want to validate a registration form that accepts an email address in the
I want to post some values from a simple HTML form, validate them with
I'm trying to validate some of the field in my form. and I want

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.