Success message after filling form with HTML and PHP

0

I'm creating a simple customer registration through a form. However, I am not able to put a "Successfully Signed" message on the same page as this form. The validation of this form, before sending to the Database, I did in PHP, according to the code below:

index.php - Where is the form and where am I trying to put the message.

            <form method="post" name="formCadastro" action="inserir_dados.php" >

            <input type="text" id="nome" name="nome" placeholder="Seu nome completo"

                    <?php
                        if(!empty($_SESSION['value_nome'])){
                            echo "value='".$_SESSION['value_nome']."'";
                            unset($_SESSION['value_nome']);
                        }
                     ?>>
                     <?php
                        if(!empty($_SESSION['vazio_nome'])){
                            echo "<p style='color: #b63e3e;position: relative; top:43px'>".$_SESSION['vazio_nome']."</p>";
                            unset($_SESSION['vazio_nome']);
                        }
                     ?>             

            <br><br><br>
            <input type="text" id="email" name="email" placeholder="Email comercial"

                    <?php
                        if(!empty($_SESSION['value_email'])){
                            echo "value='".$_SESSION['value_email']."'";
                            unset($_SESSION['value_email']);
                        }
                     ?>>
                     <?php
                        if(!empty($_SESSION['vazio_email'])){
                            echo "<p style='color: #b63e3e;position: relative; top:43px'>".$_SESSION['vazio_email']."</p>";
                            unset($_SESSION['vazio_email']);
                        }
                     ?>                     

            <br><br><br>
            <input type="text" id ="fone" name="fone" placeholder="Telefone com DDD"

                    <?php
                        if(!empty($_SESSION['value_fone'])){
                            echo "value='".$_SESSION['value_fone']."'";
                            unset($_SESSION['value_fone']);
                        }
                     ?>>
                     <?php
                        if(!empty($_SESSION['vazio_fone'])){
                            echo "<p style='font-family: Helvetica Neue; color: #b63e3e;position: relative; top:43px'>".$_SESSION['vazio_fone']."</p>";
                            unset($_SESSION['vazio_fone']);
                        }
                     ?>     

            <br><br><br>

            <input type="submit" value="Cadastrar">

        </form>

insert_dados.php - The PHP code to validate and insert the data into the DB.

<?php

session_start ();

include_once 'conexao.php';

$nome = null;
$email = null;
$fone = null;
$verifica_insert= null;

if(empty($_POST['nome'])){
    $_SESSION['vazio_nome'] = "Campo nome é obrigatório";
        $verifica_insert= " ";
}else{
    $_SESSION['value_nome'] = $_POST['nome'];
}

if(empty($_POST['email'])){
    $_SESSION['vazio_email'] = "Campo e-mail é obrigatório";
    $verifica_insert= " ";
}else{
    $_SESSION['value_email'] = $_POST['email'];
}

if(empty($_POST['fone'])){
    $_SESSION['vazio_fone'] = "Campo e-mail é obrigatório";
    $verifica_insert= " ";
}else{
    $_SESSION['value_fone'] = $_POST['fone'];
}

if ($verifica_insert != " "){

$nome = mysqli_real_escape_string($conexao, $_POST['nome']);
$email = mysqli_real_escape_string($conexao, $_POST['email']);
$fone = mysqli_real_escape_string($conexao, $_POST['fone']);

$result_sql = "INSERT INTO cliente (nome,email,fone) VALUES ('$nome','$email','$fone')";

}else{

    $url='index.php';
    echo("<META HTTP-EQUIV=REFRESH CONTENT = '0;URL=$url'>");
    exit();

}

if($conexao->query($result_sql) == TRUE){
    $url='index.php';
    echo("<META HTTP-EQUIV=REFRESH CONTENT = '0;URL=$url'>");
    exit();

}else{
    echo "Erro".$result_sql."<br>".$conexao->error;
        }
$conexao->close();

? >

    
asked by anonymous 15.04.2017 / 17:48

3 answers

1

For simple cases, use sessions, there is nothing simpler like this. After checking all of the information on the log page, you define a session variable with index erro or sucesso :

if(confirmado):
   $_SESSION['mensagem'] = "Usuario cadastrado com sucesso!";
   header("location: formularioDeRegisto.php");
   exit();
else:
   $_SESSION['mensagem'] = "Ocorreu um erro durante o registo, por favor tente novamente";
   header("location: formularioDeRegisto.php");
   exit();
endif;

And soon on the form page, somewhere in the form header, check to see if any of the messages are defined, and print it out and delete it in any way:

if(isset($_SESSION["mensagem"])):
   print $_SESSION["mensagem"];
   unset($_SESSION["mensagem"]);
endif; 
  
    
15.04.2017 / 20:10
0

After the Insert line (down there in your inserir_dados.php

Place:

<script>alert("Dados Salvos com sucesso!");</script>

It's not the "prettier" form, but quite simply that's what you should be looking for.

    
15.04.2017 / 18:17
0

In insert_data.php change this variable

$url='index.php';

by

$url='index.php?cad=ok';

in the code snippet below

if($conexao->query($result_sql) == TRUE){
$url='index.php?cad=ok';
echo("<META HTTP-EQUIV=REFRESH CONTENT = '0;URL=$url'>");
exit();

On the form page, place

if ($_GET["cad"]=="ok"){
  $texto = ("<p><b><font color=\"#FF0000\">Cadastro realizado com sucesso</font></b></p>");
}

Here you give <?php echo $texto; ?> to the exact place you want it to appear on your page.

    
15.04.2017 / 20:16