K
K
k0rabI2017-10-23 22:34:54
PHP
k0rabI, 2017-10-23 22:34:54

AJAX doesn't work. What is the reason?

New to AJAX. I'm trying to create a registration form. To do this, I want to check for repeated login.

When I enter something in #login_r, nothing happens. In general...

Actually here is a part of the form, and the script itself.

<form action="" method="POST" id="form_reg">
  <div class="form-group has-feedback">
    <div class="input-group">
            <span class="input-group-addon icon"><i class="glyphicon glyphicon glyphicon-user"></i></span> 
      <input type="login" class="form-control login_r" id="login_r" placeholder="Kot" name="login_reg" required="required">
      <span class="glyphicon form-control-feedback"></span>
    </div>
    <span id="message_log"></span>
...</form>

$(function() {
  $('#login_r').keyup(function(){
 		$.ajax({
      type: 	'POST',
      url: 	'reg.php',
      data: 	$('#form_reg').serialize(),
      success:function(msg){
        if(msg == 'y'){
          alert('y');
        $('#message_log').html('<font>Этот логин можно использовать.</font>');
        }else if(msg == 'n'){
          alert('n');
          $('#message_log').html('<font>Этот логин уже занят.</font>');
        }
      },
      error: function(msg) {
      			alert('error');
      		       }
        });
        }); 
});


And also the handler itself:
<?php
$db_host = "127.0.0.1";
$db_user = "root";
$db_password = "";
$db_table = "client";

$db = mysql_connect($db_host,$db_user,$db_password) OR DIE ("No connection");
mysql_select_db("KPMagZap", $db);

if(isset($_POST["login_reg"])){ 

    $login=htmlspecialchars($_POST['login_reg']); 
    
    $query=mysql_query("SELECT * FROM client WHERE log='".$login."'");
    $rowcat=mysql_fetch_array($query); 
    $numrows=mysql_num_rows($query); 

    if($numrows == 0) {
          echo "yes";
          }
          else{
          echo "no";
        }  	 
    } 
?>


Changed the code a little
$(function() {
    $('#login_r').on('keyup', function(){
      console.log('пошел процесс!');
 			$.ajax({
        type: 	'POST',
        url: 	'reg.php',
        data: 	$('#form_reg').serialize(),
        success:function(msg){
          if(msg == 'yes'){
            $('.message_log').html('<font color="green">Этот логин можно использовать.</font>');
          }else if(msg == 'no'){
            $('.message_log').html('<font color="green">Этот логин уже занят.</font>');
          } else {
      					console.log(msg);
      				}
        },
        error: function(msg) {
      			alert('error');
      		}
     });
    }); 
});

Now outputs console.log(msg); . That is, it skips the entire loop until the last else.
0c149d34d9ab66788282ab4bd7148e9e.png

There is an assumption that swears at these gaps. How can you fix?
53adeaadac5cc070938a778f3187052f.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
k0rabI, 2017-10-24
@k0rabI

I decided. As I said, the problem was in the space at the end ... I don’t know where it came from. I copied the code, recreated the file, and pasted it over again and voila - it worked like clockwork...)
Thank you all for your participation)

T
ThunderCat, 2017-10-24
@ThunderCat

-1) who is input type="login"???? I know text, I know password, login what kind of beast?
0) check if JQ is connected
1) bind event on loading, and it's more correct to use on, that is:

$(document).ready(function () {
    $('#login_r').on('keyup', function(){
        console.log('пошел процесс!');
        ...
        console.log(msg);
    })
}

2) Use console.log(), first at the very beginning of the js handler function, to make sure that the bind works at all, then, if everything works, display the key variables at the branch/parameter change points. At the end, display what came with Ajax.
3) as advised, for starters and for debugging, remove everything and leave only the output.
4) mysql_* functions are deprecated, use PDO (recommended) or mysqli_*.
UPD:
Now outputs alert('error');. That is, it skips the entire loop until the last else.

First, replace the alert with output to the console, then - output not a useless error, but the data that came in, why the hell do you need this eggor? According to the mind, it is necessary to operate with a number and not a letter, that is, give 0 or 1.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question