I have a login form which redirects the user to a new page if the form is submitted, however it does not show the error messages on the same page as the form. Currently if nothing is typed in user or passwords fields, the same page is relaoded process2.php showing the form. I have tried <? echo "$message"; ?>

<?php
$error_stat = 0;
$message = '';


if(isset($_REQUEST['submit'])) {

$username = $_POST['user'];
$password = $_POST['password'];


//Error checking

if (empty($username)) {
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter a username
$message = 'Please enter a username.';
}

if (empty($password)) {
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 2;

//Set the message to tell the user to enter a username
$message = 'Please enter a password.';
}

//Then, only run the query if there were no errors (if $error_stat still equals 0 redirect user)
if ($error_stat == 0) {
header("Location: process3.php");
}
}


//Then, for the form, only show it if 1) the form hasn't been submitted yet OR 2) there is an error 
if (!isset($_POST['submit']) || $error_stat == 1 || $error_stat == 2 ) {
?>


<!--this will show whatever is in the $message variable -->
<?=$message?>

<form method="post" action="process2.php">

<label for="username">Username:</label>
<input name="user" type="text" />



<label for="password">Password:</label>
<input name="password" type="password" />




<input type="submit" name="submit" value="Login" />

</p>

</form>


<?php
}
?>
</div>
</div>

Thanks

works, missed out echo for message

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.