javascript - Username availability check using AJAX -


i'm trying develop small website in php test skills. found myself stuck when decided implement live username availability check sign process, javascript knowledge basic.

right when put name in username field, loading image , nothing else. have done:

my login.php page stored in /views folder:

<section>     <div>         <form action="?action=login" method="post">             <input type="text" placeholder="username" name="username" id="username" onkeypress ="check()">             <span id="usernamestatus"></span>         </form>     </div> </section> 

my js file stored in /views/js:

function check () {     var status = document.getelementbyid('usernamestatus');     var u = document.getelementbyid('username').value;     if(u != ""){         status.innerhtml = '<img src="views/img/wait.gif" alt="" height="20" />';         var hr = new xmlhttprequest();         hr.open("post", "../login.php", true);         hr.setrequestheader("content-type", "application/x-www-form-urlencoded");          hr.onreadystatechange = function() {             if(hr.readystate == 4 && hr.status == 200) {                 status.innerhtml = hr.responsetext;             }         }          var v = "checkname="+u;         hr.send(v);      } } 

and logincontroller.php stored in /controllers:

    public function run() {         if(!empty($_post["checkname"])){             $name= htmlentities($_post["checkname"]);             if(db::getinstance()->username_ok($name)){                 echo ("<strong>" . $name . "</strong> unavailable");             }             else{                 echo ("<strong>" . $name . "</strong> available");             }         }         require_once('views/login.php');     } 

could explain me have done wrong? thanks!

i think problem filenames.

from question, guess file structure

root |-- controllers |   `-- logincontroller.php `-- views     `-- js         `-- script.js 

in javascript code in file views/js/scriptname.js,

hr.open("post", "../login.php", true); 

request sent file login.php.

but, have mentioned, path server side php script /controllers/logincontroller.php.

according me, javascript code should following:

hr.open("post", "controllers/logincontroller.php", true); 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -