Javascript alert is not working

0

Hello, I have a registration form where the password must be repeated, and all fields must be filled out, otherwise the user will remain on the page to register. However, Javascript is not working! Not even the window appears on the screen ... Is there anything that can be done to fix it? Or a way to do in PHP? Why Javascript does not work?

Here is the code:

cadastra_user.php

<?php 
    include('conecta.php');
    include('functions.php');
    include('function_usuario.php');

    $senha = $_POST['senha'];
    $senha2 = $_POST['senha2'];
    $nome = $_POST['nome'];
    $email = $_POST['email'];

    $cadastra = cadastraUsuario($conexao, $nome, $email, $senha);

    if($senha != $senha2){
        ?><script>
        alert("As senhas não conferem!");
        window.location.href="cadastro.php";
        </script>
        <?php
    }
    if($cadastra == null){
        ?><script>
        alert("Por favor, preencha todos os campos!");
        windows.location.href="cadastro.php";
        </script>
        <?php
    } else {
        $_SESSION["success"] = "Usuário cadastrado com sucesso.";
        header("Location: login.php");
    }
?>

cadastro.php

<?php 
    include ('function_usuario.php');
?>
<!doctype html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="pt-br"> <!--<![endif]-->
<head>

    <!-- Basic Page Needs
  ================================================== -->
    <meta charset="utf-8">
    <title>New Cars</title>
    <meta name="description" content="">
    <meta name="author" content="">
    <!--[if lt IE 9]>
        <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]--><!--MobileSpecificMetas==================================================--><metaname="viewport" content="width=device-width, initial-scale=1"> 

    <!-- CSS
  ================================================== -->
    <link href="http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,800,700,600" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="css/iconfonts.css">
    <link rel="stylesheet" href="css/base.css">
    <link rel="stylesheet" href="css/skeleton.css">
    <link rel="stylesheet" href="css/layout.css">

    <!-- Favicons
    ================================================== -->
    <link rel="shortcut icon" href="images/favicon.ico">
    <link rel="apple-touch-icon" href="images/apple-touch-icon.png">
    <link rel="apple-touch-icon" sizes="72x72" href="images/apple-touch-icon-72x72.png" />
    <link rel="apple-touch-icon" sizes="114x114" href="images/apple-touch-icon-114x114.png" />

</head>
<body>
    <div class="container container-twelve">
        <div class="four columns offset-by-four">
            <h1 class="titles">Cadastro</h1>
            <?php if(isset($_SESSION["success"])) {?>
                <p><?= $_SESSION["success"] ?></p>
            <?php }?>
            <?php unset($_SESSION["success"]); ?>
        </div>
        <div class="four columns offset-by-four" id ="login">
            <form action="cadastra_usuario.php"  method="post">
                <label for="nome">Nome</label>
                <input type="text" name="nome" placeholder="Digite seu nome">
                <label for="email">Email de usuário </label>
                <input type="text" name="email" placeholder="Seu email para login">
                <label for="senha">Senha</label>
                <input type="password" name="senha" placeholder="Sua senha">
                <label for="senha2">Repita sua senha</label>
                <input type="password" name="senha2" placeholder="Repita sua senha">
                <input type="submit" value="Cadastrar">
            </form>

            <p><a href="index.php"> << Voltar para o site</a></p>
            <p><a href="login.php"> Já tenho um cadastro >> </a></p>
        </div>
    </div>
</body>
</html> 
    
asked by anonymous 05.12.2014 / 18:52

1 answer

4

One problem with your solution is that you both want to use an alert and redirect the user to another page.

Here's a suggestion to solve your problems: Save the messages and send them back to the form in the event of an error. Thus, the error will appear in the form of the register itself.

See the optimized code:

<?php 
    include('conecta.php');
    include('functions.php');
    include('function_usuario.php');

    $senha  = $_POST['senha'];
    $senha2 = $_POST['senha2'];
    $nome   = $_POST['nome'];
    $email  = $_POST['email'];

    if($senha != $senha2){
        $erro = urlencode( 'As senhas não conferem!' );
        header("Location: http://example.com/cadastro.php?erro=$erro");
        die();
    }

    $cadastra = cadastraUsuario($conexao, $nome, $email, $senha);
    if( $cadastra == null){
        $erro = urlencode( 'Por favor, preencha todos os campos!' );
        header("Location: http://example.com/cadastro.php?erro=$erro");
        die();
    }

    $_SESSION["success"] = "Usuário cadastrado com sucesso.";
    header("Location: login.php");
?>

And in the cadastre.php put the PHP snippet to show the error:

        <div class="four columns offset-by-four" id ="login">
            <?php if( isset( $_GET['erro'] ) ) {
                echo '<div class="erro">';
                echo htmlentities( $_GET['erro'] );
                echo "</div>\n";
            } ?>
            <form action="cadastra_usuario.php"  method="post">
                <label for="nome">Nome</label>
    
05.12.2014 / 20:10