Xigo Xigo - 4 months ago 16
PHP Question

undefined variable login message

I have this registration page, and I call the method

reg_check()
on $user. I get errors messages, and they are displayed if some inputs haven't been filled. But when I fill all of it I get an error message "undefined variable".

I tried to put $message=" " in different places in method and regPage but still getting error: undefined variable login message

This is on registration page:


if(isset($_POST['submit'])){
//$message = "";
$user->reg_check();
}else
{
$message = "";
}
?>


Problem on this line:
NOTICE: UNDEFINED VARIABLE: MESSAGE IN...

<h4 class="bg-danger"><?php echo $message; ?></h4>


reg_check method

public function reg_check()
{


global $baza;
switch(isset($_POST)){

case empty($_POST['pass']):
// header( "refresh:4;url=reglog.php" );
$message = "Upišite šifru u odgovorajuće polje";
break;
case ($_POST['pass']<5):
// header( "refresh:4;url=reglog.php" );
$message = "Vaša šifra mora biti duža od 5 karaktera";
break;
case empty($_POST['email']):
// header( "refresh:4;url=reglog.php" );

$message = "Upišite mail u odgovarajuće polje";
break;

case isset($_POST['email']):


$email = $baza->sanitize($_POST['email']);
$cmail=['mail'=>$email];
$postoji = $baza->prep_query("SELECT email FROM users WHERE email = :mail limit 1",$cmail);
$count= count($postoji->fetch(PDO::FETCH_OBJ));

if($postoji->rowCount() > 0){
// header( "refresh:4;url=reglog.php" );
$message = "Korisnik sa email: " . $email . " već postoji!";

}
else
{ $message = "";
$this->registration($_POST);

}
break;
}

}

Answer

In order to access a variable defined and set inside you class. You need to get it out of the box with a method. You have one already $message. The only thing you need to do is return it. So that when you call the mehtod reg_check(), you get a value (the message). That way you can retrieve variables from inside your class.

<?php

public function reg_check()
{
  global $baza;

  switch(isset($_POST)){   

    case(empty($_POST['pass'])):
      // header( "refresh:4;url=reglog.php" ); 
      $message = "Upišite šifru u odgovorajuće polje";
      break;

    case($_POST['pass'] < 5):
      // header( "refresh:4;url=reglog.php" ); 
      $message = "Vaša šifra mora biti duža od 5 karaktera";
      break;

    case(empty($_POST['email'])):
      // header( "refresh:4;url=reglog.php" ); 
      $message = "Upišite mail u odgovarajuće polje";
      break;  

    case(isset($_POST['email'])):
      $email = $baza->sanitize($_POST['email']); 
      $cmail=['mail'=>$email];    
      $postoji = $baza->prep_query("SELECT email FROM users WHERE email = :mail limit 1",$cmail);
      $count= count($postoji->fetch(PDO::FETCH_OBJ));

      if($postoji->rowCount() > 0) {
        // header( "refresh:4;url=reglog.php" ); 
        $message = "Korisnik sa email: " . $email . " već postoji!";
      } else {
        $message = "";
        $this->registration($_POST);
      }
      break;
  }

  // when all of the switch() case: as been done return the message
  return $message;

}

Then on any file where you have defined $user as a new instance of you class. You can call in the method and get in return a value.

<?php

if(isset($_POST['submit'])) {
  // echo the value returned by the method `reg_check()`
  $message = $user->reg_check();
} else {
  $message = '';
}

echo $message;

Or in one line

echo isset($_POST['submit']) ? $user->reg_check() : '';