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

Asked

Viewed 27 times

1

How do I make 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>

1 answer

1

Only with PHP, one way to do this is to create a div below the form and a variable with the operation message, it must be set before the if that checks if the $_POST exist, so she ($msg) comes into existence when the form is called for the first time or avoids a variable Undefined ...

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

   </form>
   <div id="msg"><?php echo $msg; ?></div>
  • It worked here, thank you very much..

Browser other questions tagged

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