PDA

View Full Version : Store DIV data into Database


jan lee
08-23-2010, 01:34 PM
Hi

How can I add the contents from DIV ( <div id="results"></div> ) into an MSAccess database using ASP 3.0?

My script
_______





<html>
<head>
<title></title>
<script type="text/javascript">
function countWords(s) {
if (!window.wordCache) window.wordCache = [];
var arWords = s.match(/[\wáéóíñúü]+/gi);
for (var x = 0; x < arWords.length; x++) {
countWord(arWords[x]);
}
// report total counts;
document.getElementById("results").innerHTML = 'Totals:<p/>' +
'Total wordcount: ' + arWords.length + '<br/>';

for (var x = 0; x < window.wordCache.length; x++) {
document.getElementById("results").innerHTML +=
window.wordCache[x].word + ': ' +
window.wordCache[x].count + '<br/>';
}
}

function Word(val) {
this.word = val;
this.count = 1;
return this;
}

function countWord(word) {
var bExists = false;

// see if in wordCache
for (var x = 0; x < window.wordCache.length; x++) {
if (window.wordCache[x].word == word) {
window.wordCache[x].count += 1;
bExists = true;
}
}
// else add to wordCache
if (!bExists) window.wordCache[window.wordCache.length] = new Word(word);
}
</script>
</head>

<body>
<form>
<textarea name="t" rows="8" cols="35"></textarea><br>
<input type="button" name="" value="Count words" onclick="countWords(this.form.t.value);" />
<p/>
<div id="results"></div>

</form>
<body>
</html>




Jan Lee

Old Pedant
08-24-2010, 07:21 PM
Well, nothing directly to do with ASP.

In general, *NO* server can "see" the contents of a <div> in the browser.

*ONLY* data that is in the fields of a <form> can be sent to the server. (Okay, you can also do it via a query string, but that has severe limitations on the amount of data.)

So you would have to copy the data in the <div> into a form field and then submit the form.

But why would you want the TEXT that is in that <div> in the database???

Wouldn't it make more sense to instead send the <textarea> to the ASP server, have ASP count the words, and then put the words with counts into a *properly* formatted database???

jan lee
08-24-2010, 08:38 PM
I so sorry, I forgot the mention the following:

There is a form and the contents of that is redirected to an asp. file

<form name="form1" method="post" action="newfile.asp">
<textarea name="t" rows="8" cols="35"></textarea><br>
<input type="button" name="" value="Count words" onclick="countWords(this.form.t.value);" />
<p/>
<div id="results"></div>
</form>

The contents of <textarea> is correctly saved on my database, however not the data of the "results" into a "div".

jan lee

Old Pedant
08-24-2010, 10:24 PM
But my point is that the <div> will be nothing more than a block of text.

What's the point of storing that "blob" in the database???

Shouldn't you, instead, store the list of words along with their counts? Probably in a separate table?

Something like:

Main table:
id INT AUTONUMBER,
textFromTextArea MEMO

WordCount table:
id INT REFERENCES main( id ),
word TEXT,
count INT

So you'd end up storing something such as:

Main:
17 :: The name of the game is of the thing.

WordCount:
17 :: the :: 3
17 :: name :: 1
17 :: of :: 2
17 :: game :: 1
17 :: is :: 1
17 :: thing :: 1


????

Isn't that *REALLY* what you should be after?