The text box that the script is using -- its part of the form -- so you should get the word count when a user submits. If you don't -- you can try two things :
Give the text box that displays the word count a name (as in name="wordcount") -- or you can add a bit of javascript to add the final word count as a hidden form field -- which is sent along with the rest of the form data.
The first solution is the easiest since you don't have to do much. To test it out -- have your form submit to a test php script. In it, just have the following :
PHP Code:
<?php echo "<pre>"; print_r($_POST); echo "</pre>"; ?>
and it will tell you what is being sent with the form submission. If you form uses GET and not POST as the method, change $_POST to $_GET.
The second solution is better -- since you then have the option to place the running word counter outside your form, or even in a separate <div> and not have it be a text field. However, it requires a bit of editing of the javascript (actually, adding a new function), and adding a new hidden field to the form.
This opens up your form to
possible exploits -- where some malicious user can enter any artbitrary number as the length of words typed. There is an easy fix for that though. In your server side script that does the processing of the form, verify that the word count actually matches whatever data is in your textarea. With PHP, its simply a matter of using the right function (
str_word_count).
I'll write the extra bits of Javascript and the little PHP-snippet that will aid in implementing solution #2:
Code:
function addWordCount(src,dest,isHTML = false)
{
//Function to add the resulting
//Word count as a hidden value
//Argument Count : 3
//First argument is the source
//where the word count is
//displayed. The second argument
//is the hidden field which needs
//to be updated with the word count.
//The third argument is a flag -- if the source field
//is a HTML field (such a <div>, <span>, etc) then
//set it to true. If its a <input> field, leave it at false.
if (isHTML = true)
{
return dest.value = src.innerHTML;
} else {
return dest.value = src.value;
}
//Something went wrong
//so return false
//which prevents the form from
//being submitted
return false;
}
Now, the requisite changes in the form :
Code:
<form [...]>
<input type="hidden" name="hiddenwordcount" id="hiddenwordcount" value="" />
[..]
<input type="text" name="displayed" id="displayed" />
<input type="submit" [.,,] onclick="return addWordCount('displayed','hiddenwordcount');" />
Now the PHP snippet to verify the word count :
PHP Code:
$word_count = $_POST['hiddenwordcount'];
if (str_word_count($_POST['textfield'] != $word_count)
{
//Word counts don't match!
}
textfield is whatever the name of the textfield is that you are counting the words for
Let me know if you need clarifications. I wrote this at 11:21 AM -- so it might be littered with errors that tried eyes overlooked.