Mark
Mark

Reputation: 1

Store error message in a variable in native PHP

Is it possible to store error messages(Fields are empty or incorrect credentials) in a variable and just echo it in a <div>tag?

Upvotes: 0

Views: 269

Answers (1)

Sougata Bose
Sougata Bose

Reputation: 31749

Try this -

$message = "Hello";

And

<?php if (!empty( $message)) {?>
    <div><?php echo $message;?></div>
<?php }?>

if you are setting the messages on other pages then use session. Like -

$_SESSION['message'] = "Hello";

And

<?php if (!empty( $_SESSION['message'])) {?>
    <div><?php echo $_SESSION['message'];?></div>
<?php }?>

And dont forget to add session_start() at the start of the page.

Upvotes: 4

Related Questions