Erik Nucibella Erik Nucibella - 4 months ago 6x
Javascript Question

PHP - How to return an error message to a register page

i'm having issues in returning an error message into a register form: I have 2 files (register.php and process_register.php),
Pratically into register.php ther's the form from which i pass the request thought POST, while in process_login is executed a function (register($conn,$username,$password))

Here are the code of the files


if((include 'db_connect.php')==TRUE){echo"OKDB";}
if((include 'functions.php')==TRUE){echo"OKFUNCTIONS";}

if(login_check($conn) == true) //Already Logged In!
header('Location: ./index.php');

if(isset($_POST['username'], $_POST['password'])) {
//Got username and pswd
$password = $_POST['password'];


if($registerresult==TRUE) //Registration success!
header('location: login.php');
header('location: register.php');

}else {
// Not here with a post request.
echo 'Invalid Request';



function register($conn,$username,$password){


$alreadyregister="SELECT * FROM User WHERE username='$username';";

if($alreadyregister_rows!=0) //UsernameAlreadyUsed
return false;
else{ //UsernameNotAlreadyUsed

$registration="INSERT INTO User(username,password) VALUES('$username',sha2('$password',512));";
return true;

} //Register

My question is: How can i show a message into the form if the username is not 'correct?(returns false)
Cheers in advance!


This is quite simple just add these lines in your existing code

change this header('location: register.php'); to

 header('location: register.php?message=username already exist');

and in your register.php check for this

<?php if(isset($_GET['message'])) {echo $_GET['message'];} ?>