Friends, I have such a question! There is a registration form! This ajax script is hung on it

 $.ajax({ type: "POST", data: "key=" + em, url: "/proverka.inc.php", dataType: "json", success:function(data){ if(data.key == "error") { $("#proverka2").text("Данный email уже используется!"); } else { $("#proverka2").text("ok"); } } }); 

it is sent to php script

 <?php session_start(); include('dbcon.inc.php'); if($_POST) { $key = $_POST['key']; mysql_select_db(DB_NAME); $query = "SELECT * FROM users"; $result = mysql_query($query); mysql_close(); while($myrow = mysql_fetch_assoc($result)) { if($myrow['email'] == $key) { $key = array("key" => "error"); echo json_encode($key); } } } ?> 

the problem is that it works, when you redial the same mailbox, a notification goes out, saying that such an email already exists, but if you enter the original email (which is not in the database), the email does not change and the notification does not disappear. Here would be to me)

  • What falls in the console? What error message. Most likely, since the script does not return anything, the data in the success function is undefined or null, which leads to an error in the data.key line and the code is not executed any further. I recommend to study purpose WHERE in SQL. - ReinRaus
  • data.key is just coming back! the first if triggered els doesn't work why? - Sasha
  • Try this: $ response = Array ("key" => ""); while ($ myrow = mysql_fetch_assoc ($ result)) {if ($ myrow ['email'] == $ key) {$ response ["key"] = "error"); }} echo json_encode ($ response); - ReinRaus
  • and mine problem in ajaxe)) - Sasha

1 answer 1

For example:

 $result = mysql_query("SELECT * FROM `users` WHERE `email` = '$key'"); mysql_close(); if(mysql_num_rows($result) != 0) { $key = array("key" => "error"); echo json_encode($key); } 
  • php and it works for me) if (data.key == "error") {$ ("#proverka2"). text ("This email is already in use!"); } else {$ ("# proverka2"). text ("ok"); } if in Ajax also works! but else does not work! for example, I am typing an email pupkin@mail.ru, if it is in the database, then "This email is already in use!" after I enter sumkin@mail.ru, nothing changes the warning and it stands still ... - Sasha