March 7, 2012
FInding duplicates in db values
Question by santa
I have a table with the following fields:
id
project
projectName
There are two different way when I display this data:
-
Before I store it into a database I place projectName values into text fields. In this case I need to be able to somehow mark when there are duplicated with some sort of jQuery code, and
-
I output the values from db into a list. Again, I need to somehow catch duplicates and mark them somehow, perhaps changing text color.
There could be multiple duplicate sets.
How can I do that?
Answer by Starx
Ok, you can send an ajax request to the server and see if is actually duplicate. I will give a simple example of how we can get something like this to work.
HTML
<a id="check"> check availabilty</a>
JQuery
$("#check").click(function() {
$.post("checkname.php",
{ name : $("#textboxname").val() },
function(data) {
//data will contain the output from the file `checkname.php`
if(data=="ok") { //imagine the case it output ok if not duplicate is found
alert('ok');
else {
alert('duplicate name exists');
}
);
});
PHP checkname.php
$name = $_POST['name'];
//now confirm this with a query or just use your own logic
//
//
if($resultfound) { echo "ok"; } else { echo "no"; }
Note: This is a very basic example to illustrate the process.