I have several fields that the user enters. All fields are in form. By clicking on the send button, I make a POST request to the server. On the server, the data is checked for correctness. Question: how to make it so that if the data is entered incorrectly, the form does not fold? Now my click to "send" reloads the entire page. How to fix it?

UPD In the client code, it is impossible to validate values, for example, captcha.

    3 answers 3

    You can either send the form by Ajax, or send the form to the same page and output the values ​​from $ _POST when the form is displayed. Example: <input type="text" name="param1" value="<?=$_POST['param1']?>"> .

    • The most reasonable way of the proposed. - oleg_ismaylov
    • I do it always :) - Zowie
     $('#contacts button').click(function(){ //собираем введеные значения используя аттрибуты name var name = textBoxes.filter('[name=name]').val(); var mail = textBoxes.filter('[name=mail]').val(); var subject = textBoxes.filter('[name=subject]').val(); var msg = textBoxes.filter('[name=msg]').val(); if(name==""||mail==""||subject==""||msg==""){ $('.error').show(300) } else { $('.error').hide(300); //самая простая валидация,использовать на рабочем сайте не рекоммендуется $.getJSON('letter.php', {name:name,mail:mail,subject:subject,msg:msg}, function(obj){ //функция отвечающая за успех сюда присылается ответ в формате json от сервера $('#answer').html(obj); }); } return false }); 

    Took from the test task for js junior.

      You can disable the button initially, then use the jQuery Validation plugin. Watch for changes in the fields and, if everything is correct, make the button clickable.

      • Updated the question. - Nicolas Chabanovsky