0

I cannot get this script work. I try to warn if login that user entered is available. But I cannot manage this script to work:

$( "#myRegForm" ).submit(function( event ) {

    var errors = false;
    var userAvi = true;
    var loginInput = $('#login').val();

    if( loginInput == ""){
        $("#errorArea").text('LOGIN CANNOT BE EMPTY!');
        $("#errorArea").fadeOut('15000', function() { });
        $("#errorArea").fadeIn('15000', function() { });

        errors = true;
    }
    else if(loginInput.length < 5 ){
        $("#errorArea").text('LOGIN MUST BE AT LEAST 5 CHARACTERS!');
        $("#errorArea").fadeOut('15000', function() { });
        $("#errorArea").fadeIn('15000', function() { });
        errors = true;
    }
    else if (loginInput.length >=5) {
        $.post('checkLogin.php', {login2: loginInput}, function(result) {

            if(result == "0") {
                alert("this");              
            }
            else {
                alert("that");
            }
        });                
    }

    if (errors==true) {
        return false;
    }
});

Everything works fine until loginInput.length >=5 else block. So I assume there is a problem with getting answer from PHP file, but I cannot handle it, though I tried many different ways. Here is checkLogin.php's file (note that jQuery script and PHP file are in the same folder):

<?php
include ("bd.php");

$login2 = mysql_real_escape_string($_POST['login2']);

$result = mysql_query("SELECT login FROM users WHERE login='$login2'");

if(mysql_num_rows($result)>0){  
    //and we send 0 to the ajax request  
    echo 0;     
}   

else{  
    //else if it's not bigger then 0, then it's available '  
    //and we send 1 to the ajax request  
    echo 1;  
}  

?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Amal.A
  • 29
  • 1
  • 1
  • 10
  • Hi @Barell - please see the edit history for my edits that you've overwritten on this question. It's a good idea to refresh your screen prior to making an edit, to ensure you have the latest version (Stack Overflow should update the page via a socket to let you know it has changed - not sure why it did not for you). Also, it's generally thought not to be a good idea to reformat code, as it might mask the source of the problem. – halfer Mar 24 '14 at 22:40
  • change your php echo from echo 0 to echo "0" – Pluda Mar 24 '14 at 22:44
  • @Pluda i guess that's not a problem, but i did and nothing changes =( – Amal.A Mar 24 '14 at 22:47

2 Answers2

0

You're literally sending the string 'loginInput'.

change

$.post('checkLogin.php', {login2: 'loginInput'}, function(result) {

to

$.post('checkLogin.php', {login2: loginInput}, function(result) {

Edit

I would just comment out everything except the following for now and see if that at least works

$.post('checkLogin.php', {login2: 'loginInput'}, function(result) { // put loginInput back in quotes

       alert('#'+result+'#');  // # to check for whitespace
    });   
andrew
  • 9,313
  • 7
  • 30
  • 61
  • that was a typo in a question sry about that, still not getting alert messages – Amal.A Mar 24 '14 at 22:38
  • also, avoid using mysql_*, it is deprecated, can lead you to security issues, use pdo instead – Pluda Mar 24 '14 at 22:40
  • 1
    what do you see in the response of the network tab of the developer console when you submit the form? – andrew Mar 24 '14 at 22:41
  • @andrew no error messages, like this part of code is being ignored – Amal.A Mar 24 '14 at 22:44
  • @Amal.A so do you see an ***outgoing*** message in the network tab when you submit the form or nothing at all? – andrew Mar 24 '14 at 23:05
  • @andrew as i have another php file that checks forms in servers side i see it's response. But no messages from `checkLogin.php` – Amal.A Mar 24 '14 at 23:08
  • I don't understand, is that what bd.php does? does that produce some output before the expected 0 or 1? – andrew Mar 24 '14 at 23:12
  • @andrew it's just used for connection nothing is produced there – Amal.A Mar 24 '14 at 23:16
  • @andrew so i commented out all the code except request and now i see post message in red color – Amal.A Mar 24 '14 at 23:21
  • if its red it probably gives you a n error code 500? 404? – andrew Mar 24 '14 at 23:28
  • I don't know where you're looking but view the page in chrome, hit f12 and view the network tab, it should show you all the info you need – andrew Mar 24 '14 at 23:42
  • @andrew, no errors but shows this `Status: Cancelled` – Amal.A Mar 24 '14 at 23:58
  • strange take a look here http://stackoverflow.com/questions/12009423/what-does-status-canceled-for-a-resource-mean-in-chrome-developer-tools – andrew Mar 25 '14 at 00:02
0
<?php
include ("bd.php");

$login2 = mysql_real_escape_string($_POST['login2']);

$result = mysql_query("SELECT login FROM users WHERE login='$login2'");

if(mysql_num_rows($result)>0){  
    //and we send 0 to the ajax request  
    echo "0"; // for you to use if(if(result == "0") you should send a string    
} else {
    //else if it's not bigger then 0, then it's available '
    //and we send 1 to the ajax request
    echo "1";  

} ?>

Pluda
  • 1,479
  • 6
  • 21
  • 45
  • I really appreciate any help i can get but i believe there is no technical difference in using `""` or `''` or not using them as echo function works fine with both of them – Amal.A Mar 24 '14 at 22:52
  • i believe the problem is not the echo function, echo always works, but jquery is waiting for a response, then it has to check if result == "0". I believe echoing just zero is the same as echo false, so in js you would have if(result == false) (sorry if i'm wrong) – Pluda Mar 24 '14 at 22:58