1

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 3

1

Probably the easiest way would be to fire a XHR (AJAX) request to your server (PHP script) that contains the ID, and then check for the existence of that ID on the server.

Sign up to request clarification or add additional context in comments.

Comments

1

You can do this using ajax.

using jQuery for example:

$.post("check.php",{id: your_var},function(data){
  alert(data);
});

check.php holds your php code that checks the db.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.