Good day. I am just starting to learn AJAX and stumbled upon a problem: How to link an event to a button so that at the same time there is an entry in the database table and printed out?

Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants Denis Bubnov , rjhdby , Sasha Omelchenko , Vadizar , ThisMan 6 Mar '17 at 4:08 .

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

    When you click the submit form button, you can send your data to the server and print the block:

    $(function() { $('form').submit(function(e) { var $form = $(this); $.ajax({ type: $form.attr('method'), url: $form.attr('action'), data: $form.serialize() }) .done(function() { console.log('success'); }) .fail(function() { console.log('fail'); }); printDiv("divName"); //id div блока //отмена действия по умолчанию для кнопки submit e.preventDefault(); }); }); function printDiv(divName) { var printContents = document.getElementById(divName).innerHTML; var originalContents = document.body.innerHTML; document.body.innerHTML = printContents; window.print(); document.body.innerHTML = originalContents; }