Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

595
Visualizações
I cannot send form by using sweetalert 2

I read a lot of posts on this but nothing work on my side

I have the follow php code:

<?

if( isset($_POST['btn-login']) ) {    
$mails = new PHPMailer;
$content = "xxxx";
$mails->IsSMTP();
$mails->send();
}

?>


<form name="form1" id="form1" class="form1" method="post">
<input class="form-control"  name="oggetto" id="oggetto" required placeholder="Oggetto"  >
<input class="btn-login" type="submit" name="submit" id="btn-submit" value="Invia"/>
</form>

with this script:

<script>

  $('#btn-submit').on('click',function(e){
                e.preventDefault();
                var form = $('.form1');
                console.log("hello");
                swal.fire({
                    title: "Inviare il msg?",
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "Si",
                }).then(function (result){
                    if(result.value === true){
                    console.log(form);
                    $('.form1').submit();
                  }
                });
            });
</script>

i can see the popup with sweet alert but when i click on the submit button the form doesn't work. Pls don't consider parameters of PHPMailer (that are just to understand the structure)

JFiddle example is here

Any help on this? thanks in advance

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The NUMBER ONE error is having ANYTHING with the reserved word submit as id or name in your form.

If you plan to call .submit() on the form AND the form has submit as id or name on any form element, then you need to rename that form element, since the form’s submit method/handler is shadowed by the name/id attribute.

Also, since the form is submitted via javascript, for some reason, the submit button will not be sent to the server so if( isset($_POST['btn-login']) ) will not work. You can instead put that name value in a hidden input field and use it on the server.

Lastly, for the HTML5 validation(required, etc.) to work, you should instead listen to a submit event on the form itself rather than the click event on the submit button.

This worked for me

$(document).ready(function(){
    $('#form1').on('submit', function(e){
        let $form = $(this);

        if( this.checkValidity() && $form.data('valid') !== true ){
            e.preventDefault();

            swal.fire({
                title: "Inviare il msg?",
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Si",
            }).then(function (result){
                if(result.value === true){
                    $form.data('valid', true).submit();
                }
            });
        }
    });
});
<?php

if( isset($_POST['submit-login']) ){
    print_r($_POST);
}

?>

<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/11.1.9/sweetalert2.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10/dist/sweetalert2.all.min.js"></script>

<form id="form1" class="form1" method="post">
    <input type="hidden" name="submit-login">
    <input class="form-control"  name="oggetto" id="oggetto" required placeholder="Oggetto">
    <input type="submit" class="btn-login" id="btn-submit" value="Invia" />
</form>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda