-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?

14
  • 1
    ajax. Your Target. w3schools.com/ajax/default.asp Commented Sep 26, 2013 at 15:44
  • 1
    Are you really asking for an AJAX tutorial? Or for someone to hand you the code? Commented Sep 26, 2013 at 15:44
  • As above, this screams for an ajax request :) Commented Sep 26, 2013 at 15:44
  • i would really appreciate a help in code to understand what am i dealing with. Commented Sep 26, 2013 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. Commented Sep 26, 2013 at 15:47

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.