Answer the question
In order to leave comments, you need to log in
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');
}
});
});
});
<?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";
}
}
?>
$(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');
}
});
});
});
Answer the question
In order to leave comments, you need to log in
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)
-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);
})
}
Now outputs alert('error');. That is, it skips the entire loop until the last else.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question