-5

I have a file where I put all my needed functions. The one that I need is this one:

//the functions file
//........
function user_exists($username){
    $username = sanitize($username);
    $query = mysql_query("SELECT COUNT('user_id') FROM users WHERE username = '$username'");
    return (mysql_result($query, 0) == 1) ? true : false;
}
?>

I have included the functions file in my register.php file. What I want to do is call that function from javascript:

//the register file (functions file is included here)
<script>
 //.....
      if(user_exists(element.value)){  ........   }
 //.....
</script>

I think this can be solved with ajax but how to do it exactly since I'm not familiar with it?

Nigel B
  • 3,577
  • 3
  • 34
  • 52
  • 1
    ajax. Your Target. http://www.w3schools.com/ajax/default.asp – Jan Czarny Sep 26 '13 at 15:44
  • 1
    Are you really asking for an AJAX tutorial? Or for someone to hand you the code? –  Sep 26 '13 at 15:44
  • As above, this screams for an ajax request :) – Daniel Dawes Sep 26 '13 at 15:44
  • i would really appreciate a help in code to understand what am i dealing with. – user2322972 Sep 26 '13 at 15:45
  • 1
    assign event,send ajax request to a service page,call this function on your service page,return the response and fetch it back.simple. – HIRA THAKUR Sep 26 '13 at 15:47
  • @JanCzarny you should never link to w3schools.... http://www.w3fools.com/ – Jeff Shaver Sep 26 '13 at 15:47
  • *Research before asking*, this site **is loaded with examples**. – brasofilo Sep 26 '13 at 15:48
  • There are literally too many duplicates to reference.... please just search Stack Overflow before posting – MDEV Sep 26 '13 at 15:48
  • @JeffShaver You're right, but I think that the w3c at the beginning can help. – Jan Czarny Sep 26 '13 at 15:51
  • @JeffShaver .... w3schools has plenty of usable information. Just because they're not officially W3C and don't have all the latest information, doesn't mean they're a useless resource. – MDEV Sep 26 '13 at 15:51
  • thanks for the 'not help' and bad rating, i am running out of time which didn't leave me a moment to learn ajax, any way, i will figure it out alone. close it. – user2322972 Sep 26 '13 at 15:52
  • @user2322972 It's the nature of the site - we're here to help, not code a project for you. There are loads of questions where people have asked for help with AJAX requests for you to look at. Don't get stroppy with us just because we want you to learn – MDEV Sep 26 '13 at 15:54
  • thanks i understand, i am actually a game dev and this project of web was needed in very limited time, sorry for trouble, and thanks again, i'll search : ) – user2322972 Sep 26 '13 at 15:57
  • @SmokeyPHP not a sheep. When I first started getting into all this a while back, I used them as a resource, only to find out (from experience) that a lot of information on there is just plain wrong. – Jeff Shaver Sep 26 '13 at 16:04

1 Answers1

0

You'll need to call the PHP function from within the PHP page and print the result. Then connect to it with Javascript/AJAX to get the plain text the PHP prints. Example from w3schools.

<script>
function loadXMLDoc(){
  var xmlhttp;
  var response;
  if (window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
  }
  xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
      response=xmlhttp.responseXML;
      alert(response);
    }
  }
  xmlhttp.open("GET","register.php",true);
  xmlhttp.send();
}
</script>
ow3n
  • 5,974
  • 4
  • 53
  • 51