Hello!
I need that when a user enters a login he is immediately checked via php and mysql and if so, then false if not then true. I almost wrote the script, but the problem is that I do not know how to make it so that the array of values ​​is already transferred to js.

Here is the script:

$("#name").blur(function () { if ($(this).val() != "") { var name = $(this).val(); $.ajax({ type: "POST", url: "test2.php", dataType: "json", success: function (data) { if (name == data.login) $("#go").text("Такой логин уже существует, попробуйте ввести другой!"); else $("#go").text("Этот логин свободный!"); } }); } else { $("#go").text(""); } }); 

Php file script:

 mysql\_connect("localhost", "admin", "12345"); mysql\_select\_db("test_db"); $result = mysql\_query("SELECT * FROM test"); $myrow = mysql\_fetch\_array($result); echo json\_encode($myrow); 

It is necessary that the ajax request immediately checks the array if there is such a login. In the usual ways, something didn’t work through the while (I didn’t see that clearly =)) Do not judge the script strictly, I don’t work with js for a long time =) Help, please!
ZY If you can tell everything in detail.

    2 answers 2

    Why did you even do that? Do you give the client a list of ALL users? And what will you do when you have 50M users (hypothetically)? And imagine what a huge traffic will be?

    You do not need any array. You need to transfer the user name to the server, and check it on the server and return a simple answer like success and success.

    It is clear that while on the client will not be successful if the server did not provide an array. Although this is generally the wrong approach, nevertheless, you can get ALL strings from the base and file them.

    Something like that

     $rows = array(); $result = mysql_query("SELECT * FROM test"); while ($row = mysql_fetch_array($result)) { $rows[] = $row; } echo json_encode($rows); 
    • And what in this case will need to write in js code? - MikroFF
    • check true or false in the server response and give the result - kemerov4anin

    on the server side give as:

     echo json_encode(array('success' => true, 'data' => $data)); // лично мне такой формат привычнее /* *либо просто */ echo json_encode($data); exit; // - обязательно 

    On the client side:

     success: function (data) { var result = jQuery.parseJSON( data ); // Если на стороне серевера $data есть ничто инной как массив, то for(i in result) if (name == result[i].login) ... //Если просто значение if (name == result.login) ... } 

    something like that