Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

261
Vistas
jquery form submit can't handle success/error notifications

I want to show a success notification if the submit was successful with this js notify library but it's doesn't works.

If I change the new Notify({ ... }) function to a simple alert("success"); then the alert is showing up...

But if I insert the same js code in the browser's Console then it's showing the notify ...

<form action="" method="post">
    <div class="form-group">
        <label for="title"><h6>Title</h6></label>
        <input type="text" class="form-control" name="title" id="title">
    </div>
    <div class="form-group">
        <label for="content"><h6>Content</h6></label>
        <textarea class="form-control" id="content" name="content"></textarea>
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-primary" id="edit">Save</button>
    </div>
</form>
<script>
 $("#edit").click(function() {

     var title = $("#title").val();
     var content = $("#content").val();
     $.ajax({
         type: "POST",
         url: "edit.php",
         data: {
            title: title,
            content: content
         },
         cache: false,
         success: function(data) {
            new Notify({
                status: 'success',
                title: 'Test',
                text: 'Success',
                effect: 'fade',
                speed: 300,
                customClass: null,
                customIcon: null,
                showIcon: true,
                showCloseButton: true,
                autoclose: false,
                autotimeout: 3000,
                gap: 20,
                distance: 20,
                type: 1,
                position: 'right top'
            })
         },
         error: function(xhr, status, error) {
             console.error(xhr);
         }
     });
      
});
</script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/simple-notify@0.5.5/dist/simple-notify.min.css" />
<script src="https://cdn.jsdelivr.net/npm/simple-notify@0.5.5/dist/simple-notify.min.js"></script>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This statement in your comment under the question clarifies the issue:

I want to send the form's data then the page is just reloading immadiatelly and the notify doesn't showing up

The issue is because you've not called preventDefault() on the event which is raised. Therefore the form is still submit through the standard manner and a page redirect occurs. As you're using AJAX you need to prevent this behaviour.

Also note that it's slightly more semantic to hook the event handler to the submit event of the form element instead of the click of the submit button. Try this:

$("form").on('submit', e => {
  e.preventDefault(); // stop form submission

  $.ajax({
    type: "POST",
    url: "edit.php",
    data: {
      title: $("#title").val(),
      content: $("#content").val()
    },
    cache: false,
    success: function(data) {
      new Notify({
        status: 'success',
        title: 'Test',
        text: 'Success',
        effect: 'fade',
        speed: 300,
        customClass: null,
        customIcon: null,
        showIcon: true,
        showCloseButton: true,
        autoclose: false,
        autotimeout: 3000,
        gap: 20,
        distance: 20,
        type: 1,
        position: 'right top'
      })
    },
    error: function(xhr, status, error) {
      console.error(xhr);
    }
  });
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda