Javascript Alert is not working

Asked

Viewed 2,754 times

0

Hello, I have a registration form where the password must be repeated, and all fields must be filled, 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 tidy up ? Or a way to do it in PHP? Because Javascript doesn’t work?

Here’s the code:

cadastra_usuario.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");
    }
?>

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]-->

    <!-- Mobile Specific Metas
  ================================================== -->
    <meta name="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> 
  • Instead of opening and closing tags <?php ?> just give a echo "<script> alert('Por favor, preencha todos os campos!'); windows.location.href='cadastro.php'; </script>";, try and see if it’s right.

  • As Bacco said, this HTML is not valid. The script is not inside the <head> or <body>. View the source code of your generated HTML. Play this if’s down or use a flag.

  • Or simply store the messages in a variable, and show in a prominent DIV in the body of the form itself, which is better in every way. It even gets friendlier.

1 answer

4


A problem with your solution is that you want at the same time to use an Alert and redirect the user to another page.

Here is a suggestion to solve the problems: save the messages and send them back to the form in case of an error. Thus, the error will appear on itself form criminal record.

View 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 register.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>
  • This worked in case the passwords didn’t check, but for some reason, it didn’t work in case the form went blank... because it will ?

  • Well, then you can’t know, because you didn’t put the code that tests if the form is blank. Probably, looking at your code, if there is such code, it is in "cadastraUsuario". If there is no conference, then you have to implement.

  • The simple fact of using $cadastra = null no longer tests whether the form is blank ?

  • It depends on what you have in the User function. If the function tests if it is empty and returns null, yes. Otherwise, no.

  • Thank you, now I understand !

Browser other questions tagged

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