T
T
tvelforce2015-12-13 20:54:20
PHP
tvelforce, 2015-12-13 20:54:20

How to display ajax error in block?

Good day.
Unable to display reverse text on the page.
HTML:

<div class="form_line">
        <div class="send_page button">Отправить</div> <!-- Кнопка отправки формы -->
        <div class="form_error"></div> <!-- Блок где выводится ошибка-->
</div>


Ajax:
$(function() {  
  $(".send_page").click(function(){
    $.ajax({
      type: "POST",
      url: "/main/hand.php",
      data: $(this).closest("form").serialize(),
      cache: false,						
      success: function(msg){ $(this).next().html(msg); } // берем кнопку, ищем следующий элемент, вставляем в него текст ошибки
    });
  });
});


In general, if you insert the error text directly into the block:
success: function(msg){ $(".form_error").html(msg); }

then everything works, but I have a lot of such blocks on the page.

I will be grateful for any help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2015-12-13
@tvelforce

In the $(this) ajax callback, $(this) is not a link to the
clicked
element
.

T
tvelforce, 2015-12-13
@tvelforce

Thank you very much for your help!

$(function() {  
  $(".send_page").click(function(){
    var form = $(this).closest("form");
    
    $.ajax({
      type: "POST",
      url: "/main/hand.php",
      data: form.serialize(),
      cache: false,						
      success: function(msg){ form.find(".form_error").fadeIn('show').html(msg); }
    });
  return false;
  });
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question