ajax sends the php form data to the script that processes it. I know that when successfully processed in ajax, success is called. For example, success: function(){ $("#report").text('Спасибо! Вы успешно зарегистрированы');} . And how can you display an error on the form for example "Such user is already registered"? Those. What actions need to be performed in order to transmit different types of errors with php in ajax and how to catch them? I understand this is doing error: :? In general, how to make the correct dynamic validation?

  • one
    This error is not involved, error will work if the error of the request itself happens, and you yourself have to check the existence of the user in your php file and give an answer depending on the result - Vadim Leshkevich

2 answers 2

On the server side, add errors to the response object with the 'errors' key

 $response['errors'][] = 'Error...'; return json_encode($response); 

on the client side:

 success: function(data) { // data - полученные от сервера данные if (data.errors.length > 0) { // вывод ошибок } } 

    As Vadim Leshkevich wrote, you need to process all these exceptions on php and give a valid answer to Ajax where, in turn, on js you should display these errors in a user-readable form

    • Ok, and how to give the Ajax exceptions and how to catch them in the Ajax itself? For some reason I can not find any clear examples. - rival.dm
    • you can give an array of values ​​for example on the result of the code in php, you can return such an array return $ result; where $ result = Array (error => true, pustoe_znachenie_poly_name => true); and, accordingly, on the client side, in the success: function (data) {}, the data variable will contain our $ result array and then parse it and make the necessary actions based on the data - Broouzer King
    • P. Fateev wrote a little more detail - Broouzer King