Send form and post message of "success" on your own page, and after clicking on Submit appear another button

Asked

Viewed 965 times

-1

Hello I’m new in these code things and wanted to know how I make to appear the success message after I give Submit, I want those messages that appear the green field, like a div and edit with css. And I also wish that after giving Submit appear another button like a download button

Index.php

<!DOCTYPE html>
<html>

<head>
  <meta "charset=utf-8" />
  <title>Cadastro</title>
  <link rel="stylesheet" type="text/css" href="assets/css/theme.css" />
  <script src="assets/js/jquery.js"></script>

  <!--Arquivo Ajax-->
  <script type="text/javascript">
    jQuery(document).ready(function() {
      jQuery('#ajax_form').submit(function() {
        var dados = jQuery(this).serialize();

        jQuery.ajax({
          type: "POST",
          url: "newsletter-form.php",
          data: dados,
          success: function envio() {

            var cont = "Email enviado com sucesso";
            document.getElementById("msg").innerHTML = cont;
          }

        });

        return false;
      });
    });
  </script>

</head>

<body>

  <div class="for" <div class="container">
    <div class="row">
      <div class="col-md-4">
        <div class="form-group">

          <form id="ajax_form" name="signup" method="post" action="cadastro.php">
            <div class="form-group">
              <input type="text" id="name_user" class="form-control" name="nome" required placeholder="Seu nome" />
            </div>
            <div class="form-group">
              <input type="email" id="email_user" class="form-control" name="email" required placeholder="Seu email" />
            </div>
            <div class="form-group">
              <input type="tel" id="telefone_user" class="form-control" name="telefone" required placeholder="Seu telefone" />
            </div>
            <button class="btn btn-primary btn-block btn-register">
Enviar
</form>

</div>
</div>
</div>
</div>
</div>

</body>

</html>

1 answer

0


To show the message in a floating box, you must be talking about creating a snackbar. In the code below, I added the snackbar and download button. The download button loads the page as hidden, and after the success of the ajax request, the CSS class that makes it hidden is removed by jquery and the Submit button is also removed. The code goes something like this:

// Função responsável por mostrar e remover o snackbar
function snackbar() {
    // Get the snackbar DIV
    var x = document.getElementById("snackbar");
    // Add the "show" class to DIV
    x.className = "show";
    // After 3 seconds, remove the show class from DIV
    setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
}

// Sua função que faz a requisição AJAX
jQuery(document).ready(function() {   
      jQuery('#ajax_form').submit(function() {
        var dados = jQuery(this).serialize();
        jQuery.ajax({
          type: "POST",
          url: "newsletter-form.php",
          data: dados,
          success: function envio() {
            // Mostra o snackbar na tela
            snackbar()
            // Mostra o botão de download
            $("#btnDownload").removeClass('hidden');
            // Remove o botão de enviar
            $("#btnEnviar").remove();
            var cont = "Email enviado com sucesso";
            document.getElementById("msg").innerHTML = cont;
          }
        });
        return false;
      });
    });
/* Classe para ocultar o botão de download */
.hidden {
    visibility: hidden;
}


/* CSS do Snackbar */
/* The snackbar - position it at the bottom and in the middle of the screen */
#snackbar {
    visibility: hidden; /* Hidden by default. Visible on click */
    min-width: 250px; /* Set a default minimum width */
    margin-left: -125px; /* Divide value of min-width by 2 */
    background-color: #333; /* Black background color */
    color: #fff; /* White text color */
    text-align: center; /* Centered text */
    border-radius: 2px; /* Rounded borders */
    padding: 16px; /* Padding */
    position: fixed; /* Sit on top of the screen */
    z-index: 1; /* Add a z-index if needed */
    left: 50%; /* Center the snackbar */
    bottom: 30px; /* 30px from the bottom */
}

/* Show the snackbar when clicking on a button (class added with JavaScript) */
#snackbar.show {
    visibility: visible; /* Show the snackbar */

/* Add animation: Take 0.5 seconds to fade in and out the snackbar. 
However, delay the fade out process for 2.5 seconds */
    -webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
    animation: fadein 0.5s, fadeout 0.5s 2.5s;
}

/* Animations to fade the snackbar in and out */
@-webkit-keyframes fadein {
    from {bottom: 0; opacity: 0;} 
    to {bottom: 30px; opacity: 1;}
}

@keyframes fadein {
    from {bottom: 0; opacity: 0;}
    to {bottom: 30px; opacity: 1;}
}

@-webkit-keyframes fadeout {
    from {bottom: 30px; opacity: 1;} 
    to {bottom: 0; opacity: 0;}
}

@keyframes fadeout {
    from {bottom: 30px; opacity: 1;}
    to {bottom: 0; opacity: 0;}
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title>Cadastro</title>
  <link rel="stylesheet" type="text/css" href="assets/css/theme.css" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="assets/js/jquery.js"></script>

</head>

<body>

  <div class="for">
  <div class="container">
    <div class="row">
      <div class="col-md-4">
        <div class="form-group">

          <form id="ajax_form" name="signup" method="post" action="cadastro.php">
            <div class="form-group">
              <input type="text" id="name_user" class="form-control" name="nome" required placeholder="Seu nome" />
            </div>
            <div class="form-group">
              <input type="email" id="email_user" class="form-control" name="email" required placeholder="Seu email" />
            </div>
            <div class="form-group">
              <input type="tel" id="telefone_user" class="form-control" name="telefone" required placeholder="Seu telefone" />
            </div>
            <button id='btnEnviar' class="btn btn-primary btn-block btn-register">    
Enviar</button>
            
            <button id='btnDownload' class="hidden btn btn-primary btn-block btn-register">Download</button>
            

<!-- Snackbar -->
<div id="snackbar">E-mail enviado.</div>
        
</form>

</div>
</div>
</div>
</div>
</div>

</body>

</html>

  • Thank you very much John, but and the javascript codes I put where?

  • Inside the tag <script> that you were actually using. Although, as your code grows, it is interesting you use an external file for JS code and call it in html using <script src="arquivo.js"/> for example

  • Dude I click the button and don’t go out of place

  • Did you at least manage to request the server, i.e., send the data? If so, the button is gone and everything? Did you insert the CSS as well? When I tested it here - using another URL just to pass the request - it worked normally.

  • The button isn’t gone, it doesn’t go anywhere I click on Ubmit and it doesn’t change anything

  • Right, but did the request pass or not? Do the following, change the line success: function envio() { for success: function envio(data) { console.log(data) send Ubmit and see if the request is succeeding. Next to this, add type='submit' on the send button.

  • i did it but it had no change, you have whatss? c if it has leave the number here I call you there

  • I don’t know how it’s not going, because it worked and mine didn’t

  • Weird. Did you do that thing I said and nothing showed up on the console? If it did not appear, for some reason the request is not passing, or else it is not being called.

  • This error appeared in the console POST http://localhost/Notflix2/php/nueva/newsletter-form.php 404 (Not Found) send @ jquery.min.js:4 ajax @ jquery.min.js:4 (Anonymous) @ config.js:15 Dispatch @ jquery.min.js:3 r. Handle @jquery.min.js:3

  • The error is in the request. It is returning you that the page does not exist. This is actually the URL of the api that you are making the request?

  • I changed and says I successfully sent the email, but on the console appears this error Uncaught Typeerror: Cannot set Property 'innerHTML' of null at Object. [as Success] (((index):40) at j (jquery.min.js:2) at Object.fireWith [as resolveWith] (jquery.min.js:2) at x (jquery.min.js:4) at Xmlhttprequest.<Anonymous> (jquery.min.js:4) send@(index):40 j @jquery.min.js:2 fireWith @jquery.min.js:2 x @jquery.min.js:4

  • But the message appeared "Email sent"

  • It’s because you div her with id msg does not exist. But otherwise, everything normal.

  • Oxe, who ever saw? kkk and I do what?

  • You can delete these two lines: var cont = "Email enviado com sucesso"; document.getElementById("msg").innerHTML = cont;

  • Now it worked dude haha vlw dude you deserve everything

  • Mark thread as answered there ;)

Show 13 more comments

Browser other questions tagged

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