How to display a message on the <p class="message-baron"></p> about successful submission, what is the error?

 <form> <div> <p class="message-baron"></p> <textarea class="comment_menu"></textarea> <p class="submit-kom" iid="x">Отправить</p> </div> </form> $('.submit-kom').click(function() { var commentMenu = $(this).closest("form").find(".comment_menu"); var comment = commentMenu.val(); var iid = $(this).attr("iid"); var comment_menu; if (comment != "") { comment_menu = '1'; commentMenu.css("borderColor","#DBDBDB"); } else { comment_menu = '0'; commentMenu.css("borderColor","#FDB6B6"); } if ( comment_menu == '1') { $(this).hide(); $.ajax({ type: "POST", url: "../xxx/xxx.php", data: "id="+iid+"&comment="+comments, dataType: "html", cache: false, success: function(data) { if (data == 'yes'){ $(this).show(); $('.message-baron').attr("class","message-remind-success").h‌​tml(".Ваше сообщение успешно отправлено.").slideDo‌​wn(400); } } }); } }); 
  • this in success is not what you think - vp_arth

1 answer 1

I showed you how to provide the correct this in the success handler in How to determine the pressing of one form from several, but at the same time use 1 function (common to all forms) . An error with comment / comments also migrated from that question.

 $('.submit-kom').click(function() { var commentMenu = $(this).closest("form").find(".comment_menu"); var comments = commentMenu.val(); var iid = $(this).attr("iid"); var comment_menu; if (comments != "") { comment_menu = '1'; commentMenu.css("borderColor","#DBDBDB"); } else { comment_menu = '0'; commentMenu.css("borderColor","#FDB6B6"); } if ( comment_menu == '1') { $(this).hide(); // instead of ajax - simulate asynchronous call with "bind": setTimeout((function(data){ if (data == 'yes'){ $(this).show(); $('.message-baron'). attr("class","message-remind-success"). html(".Ваше сообщение успешно отправлено."). slideDown(400); } }).bind(this), 1000, "yes"); /*$.ajax({ type: "POST", url: "../xxx/xxx.php", data: "id="+iid+"&comment="+comments, dataType: "html", cache: false, success: (function(data) { if (data == 'yes'){ $(this).show(); $('.message-baron'). attr("class","message-remind-success"). html(".Ваше сообщение успешно отправлено."). slideDown(400); } }).bind(this), error: function() { console.log("error"); } });*/ } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <div> <p class="message-baron"></p> <textarea class="comment_menu"></textarea> <p class="submit-kom" iid="x">Отправить</p> </div> </form> 

  • Thank you very much, very helpful. I just finally did not understand this and so on. I will understand, thank you - Ruslan Liashenka