I want to put an alert after sending the form to the database, with something like "Your message has been sent." [closed]

-1

I wanted when the form was sent to the Bank, an alert would appear with something like "It was right to insert" if it did not say "Error sending your message".

<?php 

    if(!empty($_FILES['uploaded_file'])){
        $username = 'root';
        $password = '';
        $connection = new PDO( 'mysql:host=localhost;dbname=bd', $username );

        $query = "INSERT INTO denuncia (descricao, imagem, id_usuario, oque_descricao,id_bloco, id_denuncia_oque) 
              VALUES (:descricao, :imagem, :id_usuario, :oque_descricao, :id_bloco, :id_denuncia_oque)";

        $statement = $connection->prepare($query);





        $path = "img_denuncia/";
        $path = $path . basename( $_FILES['uploaded_file']['name']);
        if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path))



        $valores = array();
        $valores[':descricao'] = $_POST['descricao_denuncia'];
        $valores[':imagem'] = $_FILES['uploaded_file']['name'];
        $valores[':id_usuario'] = 2;
        $valores[':oque_descricao'] = $_POST['oque_descricao'];
        $valores[':id_bloco'] = $_POST['bloco_denuncia'];
        $valores[':id_denuncia_oque'] = $_POST['id_denuncia_oque'];



        $result = $statement->execute($valores);


    }
    ?>

Here is the Form

<form id="caixa" class="center-block row col-xl-6" enctype="multipart/form-data" name="formulario"  method="POST" action="">
            <br>
              <div class="row p-0 no-margin col-12 col-sm-12  col-md-12 col-lg-10 col-xl-12" >
                <div class="form-group">
                  <label for="sel1">Bloco:</label>
                  <select class="form-control" name="bloco_denuncia" id="bloco">
                    <option value="1">Bloco - Computação</option>
                    <option value="2">Bloco - Mecânica</option>
                    <option value="3">Bloco - Química</option>
                    <option value="4">Bloco - Administrativo</option>
                    <option value="5">Biblioteca </option>
                    <option value="6">Ginásio </option>
                    <option value="7">Auditório</option>
                    <option value="0">Outros</option>
                  </select>
                  </div>
              <div class="form-group">
                  <label for="sel1">O que:</label>
                  <select class="form-control" name="id_denuncia_oque" id="sel1">
                    <option value="1">Sala</option>
                    <option value="2">Banheiro(térreo)</option>
                    <option value="3">Banheiro(Superior)</option>
                    <option value="4">Labóratorio</option>
                    <option value="5">Coordenação</option>
                    <option value="6">Gabinete</option>
                    <option value="7">Telecom</option>
                    <option value="8">Outros</option>

                  </select>
                  </div>

                    <div class="form-group">
                      <label for="usr">Qual:</label>
                      <input type="text" class="form-control" id="usr" name="oque_descricao" placeholder="ex: ar-condicionado ">
                    </div>

              </div>
               <textarea  id="form-control"class="noresize  col-12 col-sm-12 mb-12 col-md-12 col-lg-10 col-xl-12 " name="descricao_denuncia" placeholder="Responda a denúncia aqui! " id="denuncia" rows="13">
               </textarea>

               <br>
               <div class="row p-0 no-margin col-12 col-sm-12  col-md-12 col-lg-10 col-xl-12">  
                    <div class="botao p-0 no-margin col-6 col-sm-6 mb-3 col-md-6 col-lg-2 col-xl-10">
                   <label class="file-upload btn btn-primary">
                        Escolha o arquivo... <input  type="file" name="uploaded_file"/ accept="image/*">
                    </label>  
                   <small class="form-text text-muted">As suas mensagens não serão totalmente anônimas.</small>
                    </div>
                  <div class="botao p-0 no-margin col-6 col-sm-6 mb-3 col-md-6 col-lg-10 col-xl-2 text-right ">
                       <input id="b_enviar" type="submit" class="btn btn-success" value="Enviar" name="enviar"/>
                  </div>insira o código aqui
              </div>
            </form>
    
asked by anonymous 01.10.2018 / 21:10

2 answers

0

Or you do as @Reignomo said, or you make a return to a page passing through parameter via get a type number

if(deucertonophp){
  header('Location: http://www.home.php?msg=1');
}else{
  header('Location: http://www.home.php?msg=2');
}

and then in the home you leave a php code like this

if(isset($_GET['msg'])){
   if($_GET['msg'] == 1){
     echo "Deu certo! parabens";
   }
   elseif($_GET['msg'] == 2){
     echo "Deu errado! tente de novo";
   }
}

Besides these echo, you can assign the $ _GET value to a php variable, and work with it in a modal eg.

    
01.10.2018 / 22:59
1

You can do this with AJAX as follows;

if( $result = $statement->execute($valores))
{
 echo 1; // dados enviados com sucesso
}
else
{
// na verdade o else não é necessário mas se preferir pode colocar
 echo 0; // erro ao tentar enviar dados 
}

The php page you're in forms an ajax to display the alert, something like;

$('#form').submit(function() {
        $.ajax({ 
            data: $(this).serialize(), 
            type: $(this).attr('method'),
            url:"PAGINA PHP COM QUE ENVIA OS DADOS",
            success: function(retorno) {
                   if(retorno==1)
                  {
                    alert("Formulário enviado com seucesso");
                  }
                   else
                  {
                    alert("erro ao enviar formulário");
                  }
            }
        });
        return false; 
    });
    
01.10.2018 / 21:42