How can I hide or show a div from one page to the other?

Asked

Viewed 41 times

0

I have a file where people write in the camps:

<div class="banner-bot" >
<div class="container">
    <h2>Registar</h2>
    <p>Preenche os dados para criar a tua conta. Quando te registares irá ser enviado um email para confirmares a conta. </p>
    <br>
</div>
<form name="registarUtilizador" action="Inserir/InserirUtilizador.php" onsubmit="return validarRegisto()" method="POST">
    <div class="register-box">
        <div class="text">
            <input type="text" placeholder="Nome Completo" required=""  name="nomeCompleto" id="nomeCompleto"  maxlength="99"/>
            <br>
            <br>
            <input type="text" placeholder="Email" required="" name="email" id="email" maxlength="99"/>
            <br>
            <br>
            <input type="text" placeholder="Confirme o seu Email" required="" name="emailConfirmar" id="emailConfirmar" maxlength="99"/>
            <br>
            <br>
            <input type="password" placeholder="Password" required="" name="pass" id="" maxlength="20"/>
            <br>
            <br>
            <input type="password" placeholder="Confirme a sua Password" required="" name="passConfirmar" id="passConfirmar" maxlength="20"/>
            <br>
            <br>
        </div>
        <div class="text-but">
            <input type="submit" name="submit" value="Confirmar"/>
        </div>  
    </div>
</form>

And when the Submit button is clicked, the format of the email will be checked, if the email already exists in the database... all in another file. And what I wanted was to create a div or an Alert, that is, a way to inform that the email is already registered and return to the registration page. And the same when the registration is successfully done. Return to the main page warning the user that your account has been successfully registered.

Verification code:

<?php require '../functions.php'; ?>
<body>
<?php

$nomeCompleto = $_POST["nomeCompleto"];
$email = $_POST["email"];
$pass = $_POST["pass"];

$options = [
    'cost' => 12,
];
$pass = password_hash($pass, PASSWORD_BCRYPT, $options);

// Create connection
$conn = db_connect(); 

$sql = "INSERT INTO utilizadores (nomeCompleto, email, pass) 
VALUES ('$nomeCompleto', '$email', '$pass')";

if ($conn->query($sql) === TRUE) {
    // Retornar para a página index e avisar o utilizador que foi registado com sucesso
    header("Location: ../index.php");
} else {
    //Retorar para a página registar e avisar o utilizador que o email já está inserido/registado
    header("Location: ../registar.php");

}
$conn->close();
?>
  • I believe there are two ways to do this. The first is with Ajax, the second is on the page register you have a get prepared and do something like this: Location: ../registar.php?value=fail

1 answer

1

you can treat these messages by "GET" I’ll show you:

in that part of your code puts something like:

if ($conn->query($sql) === TRUE) {
    // Retornar para a página index e avisar o utilizador que foi registado com sucesso
    header("Location: ../index.php?msg=sucesso");
} else {
    //Retorar para a página registar e avisar o utilizador que o email já está inserido/registado
    header("Location: ../registar.php?msg=erro");

}

Now on your registration and index page you put something like:

<?php
 if(isset($_GET["msg"]) && $_GET["msg"] == "sucesso") {
    //Aqui você coloca código html com formatação de msg
 }
?>

Basically, if you have get it will show the content of if

I hope it helps!

  • Thank you very much, it worked perfectly. I also wanted to ask if it is possible to make it impossible for users not to access this page (register.php?msg=error) without being due to registration error.

  • In this case I can only think of an alternative, an ajax request, so you can return a part of an entire html code for your "register.php", a read on ajax request, anything I can give you a help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.