Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery if/else statement with .get not working
    text
    copied!<p>On a new-user registration form, I have a real simple function just to check to see if a username exists in the database. Unfortunately, the <code>else</code> part of the statement isn't working. The PHP file works fine and returns either <code>true</code> or <code>false</code>, as expected. Here's the jQuery:</p> <pre><code>$('#username').keyup(function() { var inp = $('#username').val(); $.get('register-check-username.php', { username: inp }, function(data) { if(data!="false") { $('#namecheck').html('✓'); } else { $('#namecheck').html('This username is already taken'); } }); }); </code></pre> <p>If it's not clear, <code>username</code> is the id of the text input. When it's changed the php is checking username availability. Then a <code>&lt;span&gt;</code> tag with id "namecheck" is to change to tell the user what's up. Now, I can change <code>if(data)</code> to <code>if(!data)</code> and it will behave as expected for that change. As it is above, I can get <code>$('#namecheck').html('✓')</code> to happen when it's supposed to, but never the second part of <code>$('#namecheck').html('This username is already taken')</code>. </p> <p>Why is my <code>else</code> line never being called? I've wasted too much time trying to figure this out on my own so now I'm asking you all for some help in figuring out what's going wrong.</p> <p>edit: With <code>console.log(data)</code> in there, the console returns <code>false</code> when it's false, but nothing is logged when it's true. </p> <p>Updated <code>if(data)</code> to <code>if(data!="false")</code>. The problem remains.</p> <p><strong>UPDATE</strong> The problem isn't quite what I thought it was. It looks like the script <em>is</em> firing as it should, except it only does <code>.keyup</code> if the input value is 4 characters or less, and the username I've been testing it with is 6 letters. The data returned for the first 4 characters stays put.</p> <p>So now it looks like the problem is actually that it won't run <code>.get()</code> unless the value is 4 characters or less. I'm looking into what might be causing this in the php.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload