Jeú Casulo
Jeú Casulo

Reputation: 147

Check if input contains some value from database table

I do not know if I made myself clear in the title but I will explain again. I have the input where I will paste one string, so I want to check if contains some string:

var str1 = "ABCDEFGHIJKLMNOP";
var str2 = "DEFG";

if(str1.indexOf(str2) != -1){
    alert(str2 + " found int the input!");
}

but this way I have to pre define the str2, when what I need is to compare with table values, imagine I have a table:

So now I paste the string and ajax goes into database returning the raws:

var str1 = "ABCDEFGHIJKLMNOP";
var str2 = dataFromTable;

if(str1.indexOf(desc) != -1){
    alert("This is a " + name);
}

So any help would be great, does not need to be exaclty like that, only need to compaire value FROM database and return something. Thank you

Upvotes: 0

Views: 434

Answers (1)

Zakaria Acharki
Zakaria Acharki

Reputation: 67525

Return data in Json format and loop through it using map and compare your strings :

var data = [{id: 1, desc: "banana apple orange", name: 'fruits'},{id: 2, desc: "batman superman finn", name: 'heroes'}];
var str1 = "ABCDEFGHIJKLMNOP";

data .map(function(obj) { 
    if(str1.indexOf(obj.desc) != -1){
         alert("This is a " + obj.name);
    }
});

Hope this helps.

Upvotes: 1

Related Questions