There is a form with a field for text and input:

<form action="?????" method="POST"> <textarea>Заметка</textarea> <input class="inp1" type="button" name="sen_report" value="Добавить заметку"> </form> 

I need to, when I press the buttons, the data immediately fly into MySQL, but how to do it? if you specify the processing file in the action, the user will not be transferred to him at first, even if there is a redirection to the header

  • You need to use ajax - zenith
  • in action you can specify the page itself as a handler - user33274
  • Well, yes, but the same file is a handler for another page, how to make it clear how the script should be executed? Is it possible to specify a function in an action and in the same file write a function to add to mysql from the text entry field? - qaz qaz
  • Sorry I do not umenyu in Ajax, and means pkhp way? - qaz qaz
  • don't confuse front-end with back-end - Invision

2 answers 2

Connect the jQuery library and assign a unique identifier to the form

 <form id="note" action="" method="POST"> <textarea name="note">Заметка тест</textarea> <input class="inp1" type="submit" name="sen_report" value="Добавить заметку"> </form> 

And send an Ajax request when submitting a form

 $(function() { $('#note').submit(function() { $.post($(this).attr('action'), $(this).find('textarea,input').serialize(), function(result) { alert('success'); }); return false; }); }); 

Demo: http://codepen.io/anon/pen/greVYL

  • Corrected the demo, output the result of the ajax query in div.result - Invision

Consider using $_POST as part of your script, or write to AJAX in a separate script, which would be better in any case. The PHP solution will look something like this:

 <?php if (isset($_POST['sen_report']) && !empty($_POST['sign'])) { # выполнить MySQL запрос по нажатию кнопки } else {?> <form method="POST"> <textarea name="sign">Заметка</textarea> <input class="inp1" type="submit" name="sen_report" value="Добавить заметку"> </form><? } ?> 
  • What is the point of making the form code in a separate function html? - Invision
  • I agree. Deduced the code in a condition. Specified a name for the textarea so that the field contents are sent to POST. - Max
  • Okay, what's the point of using echo when you can close it after else ?> , Insert a fragment of html code and then open <?php - Invision
  • Can. But if this code is used not in the .htm * file, then it is impossible. - Max
  • Who told you that? PHP works great with html. - Invision