Validation of data with JS but if there is an error remain in the html page and do not proceed to the php page

Asked

Viewed 30 times

-2

Good afternoon I would like to know if someone knows how, after making the validations of the data in javascript show the error messages "failed to give the password" etc, or if everything is correct to proceed to the php page. Error: When I enter a wrong field it shows but immediately advances to p

<html>
<head>
  <meta charset="utf-8">
  <!--Titulo-->
  <title>BenchKing</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--Link CSS e JS ao HTML-->
  <script src="JS\register.js"></script>
  <link rel="stylesheet" type="text/css" href="CSS\registerstyle.css">
</head>
<body>
  <!--Começo da class registerbox-->
  <div class="registerbox">
    <h1>Registar</h1>
      <!--O PHP recolhe os dados do Utilizador e é chamada a função validar() -->
    <form id="registo_form" action="PHP\conexao.php" method="POST" onsubmit="return validar(this);">
      <p>Email</p>
      <!--Caixa de texto Email-->
      <input type="email" name="email" placeholder="Intruduza o seu Email">
      <p>Username</p>
      <!--Caixa de texto Username-->
      <input type="box" name="username" placeholder="Intruduza o Username">
      <p>Password</p>
      <!--Caixa de texto Password-->
      <input type="password" name="password_1" placeholder="Intruduza a Password">
      <p>Confirmar Password</p>
      <!--Caixa de texto Confirmar Password-->
      <input type="password" name="password_2" placeholder="Confirmar Password">
      <br><br>
      <!--Botão enviar ao carregar retorna algum erro encontrado pelo JS-->
      <input type="submit" name="enviar" onclick="return validar">  
      <br>
      <!--Hiperligação com a página do login-->
      <a href="Login.html"> Login </a><br>
    </form>
  </div>

</body>
</html>


function validar() {

   var formulario, email, username, password1, password2;

   formulario = document.forms["registo_form"];
   email = formulario.email.value;
   username = formulario.username.value;
   password1 = formulario.password_1.value;
   password2 = formulario.password_2.value;

    /*Testes na consola do browser
    console.log("Voce inseriu: "+email);*/

    if (email == ""){
        alert('Preencha o campo Email');
        formRegister.email.focus();
        return false;
    }

    if (username == ""){
        alert('Preencha o campo Username');
        formRegister.username.focus();
        return false;
    }else if(username.indexOf(" ") >= 0){
            alert('O Username n\u00e3o pode ter espa\u00e7os');
            formRegister.username.focus();
            return false;
    }else if(username.length > 25){
            alert ('O Username n\u00e3o pode ter mais que 25 caracteres');
            formRegister.username.focus();
            return false;
    }else if(username.length < 6){
            alert('O Username n\u00e3o pode ter menos que 6 caracteres');
            formRegister.username.focus();
            return false;
    }

   if (password1 == ""){
        alert('Preencha o campo Password');
        formRegister.password_1.focus();
        return false;
    }else{
        if(password1.length < 8){
            alert('A password tem que ter pelo menos 8 caracteres');
            formRegister.password_1.focus();
            return false;
        }else if(password1.length > 25){
                alert('A password n\u00e3o pode ter mais que 25 caracteres');
                formRegister.password_1.focus();
                return false;
        }else if (password2 == ""){
        alert('Preencha o campo confirmar Password');
        formRegister.password_2.focus();
        return false;
    }else if (password1 != password2){
        alert('As passwords t\u00eam que ser iguais!');
        return false;
    }

    }

    return false;
    
  }


<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "benchking";


$conn = mysqli_connect($servername, $username, $password, $dbname) or die ("A conexão falhou");

if (!$conn) {
    echo "Error: Falha ao conectar-se com o banco de dados MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}
 
echo "Sucesso: Sucesso ao conectar-se com a base de dados MySQL." . PHP_EOL;
 
$sql = "INSERT INTO benchking.register (email, username) 
VALUES ('boacalooaas','asdadasdd')";




if($conn->query($sql)===TRUE){
    echo "Novo registo enserido";
}else{
    echo "Error" .$sql. "<br>" .$conn->error;
}

$conn->close();

?>

1 answer

0

Try removing this part of the tag

action="PHP\conexao.php"

and add that to your validate() function, after all validations have been made:

document.getElementById("registo_form").action = "PHP\conexao.php";
document.getElementById("registo_form").submit();

Browser other questions tagged

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