show text on another page

Asked

Viewed 46 times

0

I am making the validations of the form fields, and I want to show a message in case they are wrong, as in the example:

Passwords do not match inserir a descrição da imagem aqui

This is the code of the action page, where you do the validations:

<!DOCTYPE html> 
<html> 
<head> 
<title></title> 


</head>
    <body> 


<?php

        if(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 code of the form page:

     <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 intend to display the text in red color below the respective input. How is this done? Where to write?

1 answer

2

You can create a div under each field and make an AJAX call to your PHP validations and insert the validation result within that div.

  • it is not possible to do it without using ajax?

  • Yes, you could do this direct validation by JS and leave the PHP validation to the end when the user presses the Submit button.I think it might be better that way. If you don’t like JS a lot you can use it just to check if the field is empty and leave the validation more advanced for PHP.

Browser other questions tagged

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