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 aecho "<script> alert('Por favor, preencha todos os campos!'); windows.location.href='cadastro.php'; </script>";
, try and see if it’s right.– Antony Alkmim
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.– Andre Figueiredo
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.
– Bacco