How do I make echo "Success" and "Failed to appear below form?"

1

How do I do the echo "Success" and "Failed to appear below the form?"

<?php

$conexao = mysqli_connect("localhost","root","","projeto");

if (isset($_POST['submit'])) {
  $nome = $_POST['nome'];
  $senha = md5($_POST['senha']);

  $inserir = mysqli_query($conexao,"INSERT INTO teste(nome,senha) VALUES('$nome','$senha')");

  if ($inserir) {
    echo "Sucesso";
  }else{
    echo "Falhou";
  }
}

?>

                       

<h1 id="nome">cadastro</h1>

<form id="form" action="index.php" method="post">
  <input type="text" name="nome" placeholder="Digite seu nome:">
  <input type="password" name="senha" placeholder="Crie uma nova senha:">
  <input type="submit" name="submit" value="Enviar">
</form>

    
asked by anonymous 22.09.2016 / 20:33

1 answer

1
Just with PHP, one way to do this is to create a div below the form and a variable with the operation message, it should be set before if that checks whether $_POST exists, so it ( $msg ) happens to exist when the form is called for the first time or it avoids an undefined variable ...

<?php
   $conexao = mysqli_connect("localhost","root","","projeto");
   $msg = '';
   ...linhas omitidas
   $msg = $inserir ? 'sucesso' : 'falha';
   ...linhas omitidas  

   </form>
   <div id="msg"><?php echo $msg; ?></div>
    
22.09.2016 / 20:39