Problem with function when trying to perform deletion

Asked

Viewed 97 times

2

I’m having a problem making the deletion attempt and I get an error message on the console, my script is like this:

<form action="" id="frmDeleta" class="smart-form"> 
  <button type="submit" class="btn btn-danger"> Excluir </button>
  <input name="IdEvento" type="hidden" value="<?php echo $IdEvento; ?>">
</form>
    $(document).ready(function () {             

            $("#frmDeleta")({               
                // NÃO ALTERAR O CÓDIGO 
                errorPlacement: function (error, element) {
                    error.insertAfter(element.parent());
                },
                submitHandler: function (form) {
                    var data = $(form).serialize();                 
                    // console.log(data);                   
                    $.ajax({
                        type: 'POST',
                        url: 'pDeletaAgendamento.php',
                        data: data,
                        dataType: 'json',
                        beforeSend: function () {
                            $("#msgResult").html('×AVISO! Enviando...');
                        },
                        success: function (response) {
                            if (response.codigo == "1") {
                                $("#msgResult").html('×AVISO!' + response.mensagem + '');
                                // RESETANDO O FORMULÁRIO APÓS GRAVAÇÃO
                                $('#frmDeleta').each(function () {
                                    this.reset();   
                                });

                            } else {
                                $("#msgResult").html('×ATENÇÃO! ' + response.mensagem + '');
                            }
                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            // console.warn(xhr.responseText);
                            console.log(xhr, ajaxOptions, thrownError);
                            $("#msgResult").html('×ATENÇÃO! Ocorreu um erro ao tentar enviar o Agendamento. Contate o departamento de TI.');
                        }
                    });
                    return false;
                }

            });

    }); 

But on my console I’m getting the following message:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • Have you checked if jquery is started correctly? By the error you are saying that $(...) is not a function.

  • 1

    You need to add the jQuery before executing the ajax

  • #frmDeleta refers to the name of the object form, not to the id

  • I am starting Jquery @Erloncharles and Kenny Rafael correctly

  • 2

    I hadn’t really noticed this bit: $("#frmDeleta")({, need to add some method like the $("#frmDeleta").submit({ for example, not that this deserves an answer, small syntax error, however the question is that you will have other errors in your script, keep updating there.

1 answer

2


Two errors visible in your code,

1° a tag #frmDeleta refers to a name form, not to a id

2° I believe you wanted to use the function submit using the jquery, that I know of no such call $("#frmDeleta")({ (correct me if I’m wrong)


In fact I believe you are trying to use the plugin validate, the correct syntax would be: $("#frmDeleta").validate({

$(document).ready(function() {
  $("#frmDeleta").validate({
    // NÃO ALTERAR O CÓDIGO 
    errorPlacement: function(error, element) {
      error.insertAfter(element.parent());
    },
    submitHandler: function(form) {
      var data = $(form).serialize();
      // console.log(data);                   
      $.ajax({
        type: 'POST',
        url: 'pDeletaAgendamento.php',
        data: data,
        dataType: 'json',
        beforeSend: function() {
          $("#msgResult").html('×AVISO! Enviando...');
        },
        success: function(response) {
          if (response.codigo == "1") {
            $("#msgResult").html('×AVISO!' + response.mensagem + '');
            // RESETANDO O FORMULÁRIO APÓS GRAVAÇÃO
            $('#frmDeleta').each(function() {
              this.reset();
            });
          } else {
            $("#msgResult").html('×ATENÇÃO! ' + response.mensagem + '');
          }
        },
        error: function(xhr, ajaxOptions, thrownError) {
          // console.warn(xhr.responseText);
          console.log(xhr, ajaxOptions, thrownError);
          $("#msgResult").html('×ATENÇÃO! Ocorreu um erro ao tentar enviar o Agendamento. Contate o departamento de TI.');
        }
      });
      return false;
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>

<form action="" id="frmDeleta" name="frmDeleta" class="smart-form">
  <button type="submit" class="btn btn-danger">Excluir</button>
  <input name="IdEvento" type="hidden" value="<?php echo $IdEvento; ?>">
</form>

  • Hello @Marcelo Bonifazio, thanks for the help, but I’m not wanting to use the validate, I just want to send the ID to the deletion page and receive an error return or hit.

Browser other questions tagged

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