Forum Moderators: coopster
I can't think of any better place to post this question. This is my first post here so I hope I don't mess it up.
Problem:
I'm working on this web article submit form. There are about 9 textarea boxes for input. I'm want to do a word count on all 9. I have been searching like crazy to figure this out. I have found a good example for counting 1 textarea, but I can't seem to figure out how to count them all. The textareas are named with each their own names (crude example below). Hope someone can shed some light on this for me. If I didn't explain this good, let know and I'll try better.
Example:
// 3 textarea boxes
<textarea name=intro>foo1</textarea>
<textarea name=advice>foo2</textarea>
<textarea name=prep>foo3</textarea>
//Person clicks button to do word count (opens in new window)
//Counts text from all 3 textarea boxes
//Gives total - everyone is happy!
Thank you in advance to all that reply to this post.
-webjoker
http://javascript.internet.com/forms/word-count.html [javascript.internet.com]
Possibly the same one jatar_k has lying around. :)
dc
On submit is cool with me. When I put text into the textareas I have a count button that submits the form to a count.php. I want that file to count all the text from the different textarea boxes and give a grand total. I just can't figure out how to do that. I can get it to count from one but not any more after that.
Here is the count.php:
<?php
// Textareas from Form
$intro = $_POST['intro'];
$valid = $_POST['valid'];
$prep = $_POST['prep'];
// I have this set to intro to test, but can't figure out how to add the rest
$word_count = count_words($intro);
function count_words($string) {
$string = eregi_replace(" +", " ", $string);
$string = explode(" ", $string);
while (list(, $word) = each ($string)) {
if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $word)) {
$word_count++;
}
}
return($word_count);
}
echo "You have this many word(s): $word_count";
?>
Hope this helps.
Well after messing around I found a solution that worked for me, and I thought I would share it with anyone who might find it useful.
Also like to thank jatar_k for helping me cure my headache. I tried what you said but for some reason it wouldn't output for me. So I modified the script I did have with the answer you gave me, and it worked! Wouldn't have got it with out ya. :) See I knew I came to the right forum.
Thank you again, :)
Here is my count.php revised:
<?php
// Original script by:Nick Brandt
// mod by: Webjoker
// Textareas from Form
$intro = $_POST['intro'];
$valid = $_POST['valid'];
$prep = $_POST['prep'];
// Count Function
$str = count_words($intro) + count_words($valid) + count_words($prep);
function count_words($string) {
$word_count=0;
$string = eregi_replace(" +", " ", $string);
$string = explode(" ", $string);
while (list(, $word) = each ($string)) {
if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $word)) {
$word_count++;
}
}
return($word_count);
}
echo "You have this many word(s): $str";
?>
Facing a similar problem, I solved it with a piece of javascript in the markup, like this:
<label for="someid">somelabel</label>
<textarea id="someid" name="someid" rows="somerows" cols="27" onkeyup="form_name.someid_count.value=form_name.someid.value.length;">
</textarea>
<label for="someid_count">Character count: </label>
<input type="text" size="2" name="someid_count" />
It is rather easy after that, if needed, to sum them all in another field.
Hope this'll help.
Notawiz