I have a database where a Facebook User ID, Facebook User Name and mail address is written. When the document loads I want to check if a given ID (stored in a javascript value) already exists in the database. So what I would have to do is check with PHP if my Javascript variable (value) exists in the database. Any ideas how to do this?
3 Answers
I think you need AJAX. For example:
<script>
//user id
var uid = '100';
//AJAX create
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
//GET request create and send
xmlhttp.open("GET","checkuser.php?uid="+uid,false);
xmlhttp.send();
//Good. in "xmlhttp.responseText" will store true or false (user exist or not)
alert(xmlhttp.responseText); //check answer from PHP
</script>
Ok. Now PHP script.
//this is checkuser.php
<?php
$uid = int()$_GET['uid'];
echo (bool)functionForCheckIdInDB($uid);
?>
p.s. of course with jQuery it easier.