Forum Moderators: coopster

Message Too Old, No Replies

count words for multiple textareas

         

webjoker

9:04 am on May 25, 2005 (gmt 0)

10+ Year Member



Greetings Webmaster World,

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

jatar_k

3:53 pm on May 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld webjoker (like that nick),

do you want to do it as they type or on submit?

I have a javascript bit that counts a single textarea (somewhere) as they type since there are no php solutions for client side, php could only help once they have submitted the form.

dreamcatcher

5:36 pm on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is one I got a while back off Javascript Source

http://javascript.internet.com/forms/word-count.html [javascript.internet.com]

Possibly the same one jatar_k has lying around. :)

dc

webjoker

5:39 pm on May 25, 2005 (gmt 0)

10+ Year Member



Thank you jatar_k,

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.

webjoker

5:46 pm on May 25, 2005 (gmt 0)

10+ Year Member



I have came across that one and many others like it, dc. I even tried to see if I could change that one to accept multiple textareas. Not so good in javascript, also php for that fact. :) Still learning, but not giving up! :)

jatar_k

6:21 pm on May 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



how about this function?
[php.net...]

webjoker

8:31 pm on May 25, 2005 (gmt 0)

10+ Year Member



I've looked at that and still trying to figure some of it out. I guess one of my biggest problems is combining all of the textarea boxes into one varible so I can count all the words at once.

jatar_k

8:56 pm on May 25, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$totalwords = str_word_count($intro,0) + str_word_count($valid,0) + str_word_count($prep,0);

if ($totalwords > x) echo "you talk too much";

should work ;)

webjoker

11:39 am on Jun 2, 2005 (gmt 0)

10+ Year Member



Greetings all,

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";
?>

Notawiz

5:41 pm on Jun 2, 2005 (gmt 0)

10+ Year Member



Hello everyone,

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" />

for each textarea.

It is rather easy after that, if needed, to sum them all in another field.

Hope this'll help.

Notawiz