tags:

views:

38

answers:

4

Hi all,

I am trying to load some data (which may be up to a few thousands words) from the database, and store the data somewhere in a html web page for comparing the data input by users.

I am thinking to load the data to a Textarea under Div tag and hide the the data:

<div id="reference" style="Display:none;">
    <textarea rows="2" cols="20" id="database">
        html, htm, php, asp, jsp, aspx, ctp, thtml, xml, xsl...
    </textarea>
</div>

<table border=0 width="100%">
    <tr>
        <td>Username</td>
        <td>       
            <div id="username">
                <input type="text" name="data" id="data">
            </div>       
        </td>
    </tr>
</table>

<script>
    $(document).ready(function(){
        //comparing the data loaded from database with the user's input
        if($("#data").val()==$("#database").val())
        {
            alert("error");
        }   
    });
</script>

I am not sure if this is the best way to do it. Could you give me some advice?

+1  A: 

You could use Global variable (Declare the variable above the tag) of Javascript.

Example: var sMyDatabaseVal = "whatever it coule be";

Define above varibale above your script tag and assign respective value. Replace the $("database").value() with sMyDatabaseVal.

Easwaran
+2  A: 

Why don't you store it in a global variable and compare with it?

<script>
   var originalData = "html, htm, php, asp, jsp, aspx, ctp, thtml, xml, xsl";
</script>

<script>
  $(document).ready(function(){
    //comparing the data loaded from database with the user's input
    if($("#data").val()== originalData) {
      alert("error");
    }
  });
</script>
Amarghosh
Because he doesn't know how to do that, and he probably has a busted use case model.
drachenstern
+1  A: 

Hi, When I come across such scenarios I create global JavaScript variable in a DOM and do processing over it instead of storing it in an html web page. you can put all your keywords in an array and use function in_array to check whether input from the user is present inside that global array.

<script>
var keywords = [];

function in_array(needle, haystack)
{
   for(key in haystack)
   {
     if(haystack[key] == needle)
        return true;
   }
   return false;
}
$(function(){

$.post("getKeywordsfromDatabase.php", {}, function(data){
   for(keyword in data)
   {
      keywords[keywords.length] = data[keyword];
   }
}, "json"); // you should fetch keywords as a JSON object from php using json_encode function jquery wil process it automatically if you use fourth variable as "json" in your post call.

//now you have keywords ready in a global variable 'keywords'
//so here you can check whether input from user is present in your array
if(in_array($("#data").val(), keywords)
  {alert("error");}
});


});

</script>

here you have it. Cheers Ayaz Alavi

Ayaz Alavi
A: 

you can alos use the .data() method in jquery a la:

http://api.jquery.com/jQuery.data/

this is my preferred route for this type of page context transitory data.

jim

jim