I want to put an Alert after sending the form to the database, with a response like "Your message has been sent."

Asked

Viewed 109 times

-1

I wanted when the form was sent to the Bank to appear an Alert with something like "It worked out" if it did not error "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);


    }
    ?>

This 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>
  • message of what and where?

  • Like, when I press a send button that has under my text box I want it to warn somewhere on the screen that, the message has been delivered.

2 answers

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 are the form make 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; 
    });

0


Either you do it like @Reignomo said, or you do 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 ai at 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";
   }
}

in addition to these echo, you can assign the value of $_GET to a php variable, and work with it in a modal for example.

  • But what do I put in this "DEUCERTOPHP" ? And in these "($_GET['msg'] == 2) ($_GET['msg'] == 1)" ?

  • this "deucertoPHP" is a codition whether it was inserted or not, I think putting if($statement->execute($values)){ works!

  • Ta giving error (Parse error: syntax error, Unexpected 'elseif' (T_ELSEIF), expecting ',' or ';' in) I have already taken this Else and left only the if and the reverse, but still gives syntan error. doesn’t go out of line. Please help me '-'

  • in case it would look like this ? *********if($statement->execute($values)){ header('Location: http://localhost/project/student.php?msg=1'); }Else{ header('Location: http://localhost/project/student.php?msg=2'); } if(isset($_GET['msg'])){ if($_GET['msg'] == 1) echo "It worked! congratulations" elseif ($_GET['msg'] == 2) echo "Went wrong! try again" } }

  • I edited, you’re making a mistake because I forgot the ; no echo

  • Now it worked, everything is going well, only the message "It worked out! Congratulations" "It went wrong! try again"

  • If you put the code 2 that I posted on the home, and after this process when going home it appears with url http://www.home.php?msg=1 . don’t have to give mistake, maybe you’re eating ball in something

  • What would be home ? Because I placed these two codes one from the bottom of the other inside php, if(isset($_GET['msg'])){ if($_GET['msg'] == 1){ echo "It worked! congratulations"; } elseif($_GET['msg'] == 2){ echo "Went wrong! try again"; } }

Show 3 more comments

Browser other questions tagged

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