I'm making a sign up form for a website but it doesn't wait until I press the button. When I open the page it's already saying that I haven't filled out all of the fields.
I have put an echo thing right under the part the if statement that isn't supposed to go until the button is clicked but it starts out on the screen.
if ($_POST['reg']) {
    echo "Clicked";
    if($em==$em) {
    $u_check = mysqli_query($connection, "SELECT username FROM users WHERE username='$un'");
    $check = mysqli_num_rows($u_check);
        if($check == 0){
            if($fn&&$ln&&$un&&$em&&$pswd&&$pswd2){
                if($pswd == $pswd2){
                    if(strlen($un)>25||strlen($fn)>25||strlen($ln)>25){
                    echo "The maximum limit for username/first name/last name is 25 characters!";
                    }
                    else
                    {
                    if(strlen($pswd)>30||strlen($fn)<5){
                    echo "Your password must be between 5 and 30 characters long!";
                    }
                    else
                    {
                    $pswd = password_hash($pswd, PASSWORD_DEFAULT);
                    $pswd2 = password_hash($pswd2, PASSWORD_DEFAULT);
                    $query=mysql_query("INSERT INTO users VALUES('','$un','$fn','$ln','$em','$pswd','$d','0')");
                    die ("<h2>Welcome to Notebook!</h2>Log into your account to get started...");
                    }
                }
            }
            else
            {
             echo "Your passwords don't match!";
            }   
        }
        else 
        {
         echo "Please fill in all the fields";
        }
    }
    else
    {
        echo "Username already taken...";
    }
}
}
//if($reg){
//  echo "Clicked";
//}
?>
<input type="submit" name="reg" value="Sign Up">
(note, these are just a few snippets of the code I took out)
 
     
    