I am doing the form field validations, and I want to show a message in case they are wrong, as in the example:
Thisisthecodefortheactionpagewhereyoudothevalidations:
<!DOCTYPEhtml><html><head><title></title></head><body><?phpif(isset($_POST)&&!empty($_POST)){$pass=$_POST["pass"];
$pass2=$_POST["pass2"];
if(!preg_match('/^\w{4,20}$/i',$pass)){
echo '<script type="text/javascript">if(confirm("Password inválida")){window.location.href="criar.php";}else{window.location.href="criar.php";}</script>';
break;
}else{
if($pass<>$pass2){
echo '<script type="text/javascript">if(confirm("As passwords não correspondem")){window.location.href="criar.php";}else{window.location.href="criar.php";}</script>';
break;
}
else{
$pass= hash('sha512',hash('sha512',hash('sha512',hash('sha512',hash('sha512',hash('sha512',hash('sha512',hash('sha512',hash('sha512',hash('sha512',hash('sha512',hash('sha512',($_POST["pass"])))))))))))));
}
}
$nome=$_POST["nome"];
$apelido=$_POST["apelido"];
$email=$_POST["email"];
$telefone=$_POST["telefone"];
if(!preg_match('/^\w{9}$/i',$telefone)){
echo '<script type="text/javascript">if(confirm("Telefone inválido")){window.location.href="criar.php";}else{window.location.href="criar.php";}</script>';
break;
}else{
$telefone=$_POST["telefone"];
}
$nif=$_POST["nif"];
if(!preg_match('/^\w{9}$/i',$nif)){
echo '<script type="text/javascript">if(confirm("NIF inválido")){window.location.href="criar.php";}else{window.location.href="criar.php";}</script>';
break;
}else{
$nif=$_POST["nif"];
}
$empresa=$_POST["empresa"];
$morada=$_POST["morada"];
$codigopostal=$_POST["codigopostal"];
$cidade=$_POST["cidade"];
$pais=$_POST["pais"];
include ("db.php");
$criar="INSERT INTO utilizador (nome, apelido, email, pass, telefone, nif, empresa, morada, codigopostal, cidade, pais) VALUES ('$nome', '$apelido', '$email', '$pass', '$telefone', '$nif', '$empresa', '$morada', '$codigopostal', '$cidade', '$pais')";
if ($con->query($criar)) {
//header('location: criar.php');
echo '<script type="text/javascript">if(confirm("Registo realizado com sucesso")){window.location.href="criar.php";}else{window.location.href="criar.php";}</script>';
} else {
echo "Error: " . mysqli_error($con);
}
$con->close();
}
?>
</body>
</html>
This is the form page code:
<div class="container">
<div class="card">
<div class="card-header" style="background-color:f7f7f7;">
<h2 style="color:#1751A3;">Registar</h2>
</div>
</div>
<form method="post" action="criaraction.php" id="dados">
<br>
<div class="card-group">
<div class="card">
<div class="card-header" style="background-color:#F8F9FA;" >
<h5 class="card-title">Dados Pessoais</h5>
</div>
<div class="card-body">
<!--<form method="post" action="" id="dadospessois">-->
<div class="row">
<div class="col-sm-6">
<label for="Nome">Nome:</label>
<input type="text" class="form-control" name="nome" required/>
</div>
<div class="col-sm-6">
<label for="Apelido">Apelido:</label>
<input type="text" class="form-control" name="apelido" required/><br>
</div>
</div>
<label for="email">Email:</label>
<input type="email" class="form-control" name="email" required/><br>
<div class="row">
<div class="col-sm-6">
<label for="pass">Password:</label>
<input type="password" class="form-control" name="pass" required/>
<?php include 'passred.php';?>
</div>
<div class="col-sm-6">
<label for="pass2">Confirmar Password:</label>
<input type="password" class="form-control" name="pass2" required/>
</div>
</div>
<!-- </form>-->
</div>
</div>
<div class="card">
<div class="card-header" style="background-color:#F8F9FA;">
<h5 class="card-title">Dados da Empresa</h5>
</div>
<div class="card-body">
<!-- <form method="post" action="" id="dadosempresa">-->
<label for="empresa">Empresa:</label>
<input type="text" class="form-control" name="empresa" required/><br>
<div class="row">
<div class="col-sm-6">
<label for="nif">NIF:</label>
<input type="text" class="form-control" name="nif" required/><br>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<label for="exampleFormControlSelect1">Indicativo:</label>
<select class="form-control" name="countryCode" id="indicativo">
<option data-countryCode="PT" value="351">Portugal (+351)</option>
<option data-countryCode="DZ" value="213">Algeria (+213)</option>
</select>
</div>
<div class="col-sm-6">
<label for="telefone">Telefone:</label>
<input type="text" class="form-control" name="telefone" required/><br>
</div>
</div>
<label for="morada">Morada:</label>
<input type="text" class="form-control" name="morada" required/> <br>
<div class="row">
<div class="col-sm-6">
<label for="codigopostal">Código postal:</label>
<input type="text" class="form-control" name="codigopostal" required/><br>
</div>
<div class="col-sm-6">
<label for="cidade">Cidade:</label>
<input type="text" class="form-control" name="cidade" required/>
</div>
</div>
<div class="col-sm-6">
<label for="exampleFormControlSelect1">País:</label>
<select class="form-control" name="pais" name="pais">
<option value="PT">Portugal</option>
<option value="ES">Spain</option>
<option value="FR">France</option>
<option value="IT">Italy</option>
<option value="DE">Germany</option>
<option value="GB">United Kingdom</option>
<option value="US">United States</option>
<option value="BR">Brazil</option>
</select>
</div>
<!--</form>-->
</div>
</div>
</div>
<div class="card-footer text-right" style="background-color:#FFFFFF;">
<button type="submit" align="right" class="btn btn-primary" value="submit">Continuar</button>
</div>
</form>
</div>
I want to show the text in red under the respective input. How do you do this? Where to write?