1

I've been trying to solve this problem, but no luck.

I'm trying to make an ajax call so that when a user is registering, it will check the server to see if that username is already taken.

In the file /home/myname/public_html/final/js/checkusername.js

$(function(){

    var x_timer;
    $("#Signup_username").keyup(function (e) {
       //document.write( "HELLO");
        clearTimeout(x_timer);
        var user_name = $(this).val();
        x_timer = setTimeout( function() 
             { check_username_ajax(user_name); } 
                             , 1000);         //the function defined in setTimeout is executed after a time delay of 1000 ms
    });




     function check_username_ajax(username) {
        $.ajax({
            url: '/home/myname/public_html/final/php/usernamecheck.php',
            type: "POST",
            data: {'username':username},
          success: function(){
              $('#result').html(data); 
          },
            error: function(XMLHttpRequest, textStatus, errorThrown) { 
        alert("Status: " + textStatus); alert("Error: " + errorThrown); 
    }  
})


    }
});

here is the file usernamecheck.php:

<?php


require_once '/home/myname/public_html/final/db_connect.php';

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


   if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
        die();
  }


    $username = filter_var($_POST["username"], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH);

    $statement = $mysqli->prepare("SELECT userid FROM USER WHERE userid=?");
    $statement->bind_param('s', $username);
    $statement->execute();
    $statement->bind_result($username);
    if($statement->fetch()){
        echo('username is not available');
    }else{
        echo('username is available');
    }
}

?>

I keep getting an Error: Not Found message. I'm honestly just at a loss. Everything works fine until I do the ajax call. Please help

FrostyStraw
  • 1,628
  • 3
  • 25
  • 34

2 Answers2

1

You are not supposed to include everything. Change it relative to your domain:

url: '/final/php/usernamecheck.php'

You are including the file at server side using the full physical path is okay. But AJAX is carried out in the client side, which sees the domain and path format. So you must give the path relative to your URL. If the URL is like:

http://localhost/final/php/usernamecheck.php'

Then the solution is to change the URL.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • In the php file should I also change the require_once to '/final/db_connect.php'? I've changed both but I continue getting the same error. – FrostyStraw Dec 07 '15 at 13:56
  • @FrostyStraw You don't need to change in the PHP. Just JavaScript alone please change. – Praveen Kumar Purushothaman Dec 07 '15 at 13:56
  • This is still not working. I don't know what could be wrong. The only thing that I might think might be wrong (since it's my first time using ajax) is maybe the url for the ajax function is wrong? Should the URL be the same page where the user will enter input? For example, I have a form.php, where user will enter input, which is the one that calls the checkusername.js, which in turn calls usernamecheck.php. Does that seem correct? – FrostyStraw Dec 07 '15 at 14:01
  • @FrostyStraw Can you say what you are seeing in the console? – Praveen Kumar Purushothaman Dec 07 '15 at 14:02
  • Not sure what is meant by the console. I did look at the developer tools on Chrome and it says this: Failed to load resource: the server responded with a status of 404 (Not Found) – FrostyStraw Dec 07 '15 at 14:05
  • @FrostyStraw Ha! So you are requesting a non-existent file... The URL is wrong! – Praveen Kumar Purushothaman Dec 07 '15 at 14:07
  • yeah, I've guessed there's something wrong with my URL for a while but it continues being wrong no matter what. In my "final" directory, I have both a "php" directory and a "js" directory. I am trying to call the usernamecheck.php file in the "php" directory from the checkusername.js file in the "js" directory. idk what im doing wrong. the files definitely exist unless im seeing things – FrostyStraw Dec 07 '15 at 14:10
  • I certainly wish I knew. I'm guessing I'm not writing the URL correctly, but then I don't get how its wrong since it looks fine to me. – FrostyStraw Dec 07 '15 at 14:16
  • Yes, as @Semmix Says, try using absolute URL there. – Praveen Kumar Purushothaman Dec 07 '15 at 14:22
  • @Semmix you mean just copy that as the url? – FrostyStraw Dec 07 '15 at 14:24
  • If that is what you meant, I got this message: "XMLHttpRequest cannot load ip:port/final/php/usernamecheck.php. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource." If that's not what you meant then..im lost @Semmix – FrostyStraw Dec 07 '15 at 14:31
  • @FrostyStraw Did you anywhere see the working page? Copy that URL and paste inside the `url: 'PASTE IT HERE'`... – Praveen Kumar Purushothaman Dec 07 '15 at 14:42
  • @FrostyStraw take this : `ip:port/final/php/usernamecheck.php ` and replace the "ip" with the ip adress of your server. (if you are running locally try "localhost". then, replace the ":port" with the port of your address (some development environments use ":8080" or similar ports). the default port is 80, so if you dont see a port listed after the ip address then you can drop it and just write : `http://localhost/final/php/usernamecheck.php` – Noxymon Dec 07 '15 at 20:03
  • 2
    The problem was stupid. The name of my database from the db_connect file was $db, not $mysqli (I had gotten the code from a tutorial and forgot to acutally have the right name). Had to do a crazy amount of debugging but I found it, thanks for all the help though! – FrostyStraw Dec 08 '15 at 00:26
1

There are 3 clear problems with your approach :

The first is a security issue, in case a user stop the request, he may mainpulate the data and by so doing "User Enumeration", basically mapping all the usernames in your system - be sure to protect this (restrict the amount of times this request can be made from a single entity - on the server side of course).

Second, you are addressing the the URL from a relative path, make it an absolute path as suggested by @PraveenKumar, only in the Javascript side.

Third, for debugging purposes, instead of applying die() on the unmatched http header, return something arbitrary, it will be easier for you to understand if the issue resides there or at another part.

Feel free to comment if you run into any problems, Cheers.

Noxymon
  • 201
  • 4
  • 15