send confirmation message to the database

Asked

Viewed 30 times

0

I’m trying to insert a message into the library when it makes a record. But when sending is done the message does not appear for use. Thanks for the help!

Follow my form

<form name="Form_CRUD" method="post" action="consulta.php" >
    <div class="row">
        <div class="col-6">
            <div class="form-group">
                <label>Nome</label>
                <input type="text" id="nome_input" class="form-control" name="nome">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-6">
            <div class="form-group">
                <label>Sobrenome</label>
                <input type="text" class="form-control" id="sobrenome_input" name="sobrenome">
            </div>
            <button type="submit" class="btn btn-primary">Adicionar</button>
            <button type="submit" class="btn btn-primary">Alterar</button>
            <button type="submit" class="btn btn-primary">Excluir</button>
        </div>
    </div>
</form>
<?php
    $validarenvio = isset($_GET['s=1'])?$_GET['s=1']:'';
    if ($validarenvio):
?>
<div class=" alert alert-success" role="alert">
    <strong>Sucesso!</strong>
    Cadastro realizado com sucesso
</div>
<?php endif; ?>
<?php
    $validarenvio = isset($_GET['s=0'])?$_GET['s=0']:'';
    if ($validarenvio):
?>
<div class=" alert alert-danger" role="alert">
    <strong>erro!</strong>
    Erro de envio verifique!
</div>
<?php endif; ?>

Follow my query . php:

<?php
require_once ('classes/conecta.php'); 
$nomeCli = trim($_POST['nome']);
$sobrenomeCli = trim($_POST['sobrenome']);
$insert = "INSERT INTO clientes_cli";
$insert .= " (nome_cli, sobrenome_cli)";
$insert .= "VALUES ('$nomeCli', '$sobrenomeCli')";
$Obj_Conexao = new CONEXAO();
$inserir_dados = $Obj_Conexao->Consulta($insert);
$erro = 0;
if($inserir_dados == false){
header('Location: index.php?s=0');
} else{
header('Location: index.php?s=1'); 
}     
?>

1 answer

1


Your logic is incorrect, what you want is more or less that:

<?php
$validarenvio = isset($_GET['s']) ? $_GET['s'] : null;

if ($validarenvio == '1') {
?>

    <div class=" alert alert-success" role="alert">
        <strong>Sucesso!</strong>
        Cadastro realizado com sucesso
    </div>

<?php
} else if ($validarenvio == '0') {
?>

    <div class=" alert alert-danger" role="alert">
        <strong>erro!</strong>
        Erro de envio verifique!
    </div>

<?php
}
?>
  • Funfou but in parts! Because the error messages is appearing and should be hidden. They should appear when there is a submission! As I do now?

  • I edited the answer

  • William, Why in your logic the if has no endif?

  • William. The message still continues.

  • 1

    I prefer the syntax with brackets { } to delimit the part within the if/else

Browser other questions tagged

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