Good day.

Just started to master the technology AJAX . Completely broke my head. I do not understand how I can properly handle the response from the server.

My function:

function email() { var res = ''; // результирующая строка - пока пустая. var elem = document.getElementById('mail'); var str = elem.value; // текст из формы. var list = str.split("\n"); // разбивка... for (var i = 0; i < list.length; i++) { str = list[i]; // пустые строки пропускаем... if (str.length < 1) continue; // проверка наличия такого емайла в базе... request = $.ajax({ type: "POST", url: "email_check.php", data: "email=" + str, success: function(request, res) { if (request == "1") { res = res + str + '(есть)' + "\n"; } else { res = res + str + '' + "\n"; } } }); } elem.value = res; } 

Those. I have an editable field on the page - id='mail'; . E-mail addresses are entered there, separated by a line break. Then they are divided into separate elements by the line break symbol. Next - each such separate address - I check the server request for uniqueness. If you already have one, email_check.php will return "1", if not, then "0".
My task is to get the same email list again - for those who already have it - you need to add (есть)+\n , i.e. again get the same list with line breaks and write it back into the form in the mail element.

Thanks in advance.

  • Comments are not intended for extended discussion; conversation moved to chat . - Nick Volynkin

1 answer 1

This task is easier to solve on a server like this:

  1. We get melon from the field
  2. Helmet to server
  3. We split (split ("\ n") only on the server side, if PHP but through explode)
  4. We start the cycle in it, check it and immediately add valid variables to the variable.
  5. After success we send to the client (browser)
  6. We insert into the field

Otherwise, with a large amount of data, you arrange some DDOS on your server

  • Yes. That's right. That is how the problem was solved. Processing the entire array on the server. Thank. - Alexander