Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2002
    Location
    Toronto, Canada
    Posts
    11,059

    Looking for script for calculating word count

    I need to build a form in which it automatically counts the number of words entered in a text box. Preferably, I'd like the word count to display in real time on the page as the user is adding more words.

    I tried looking at Hotscripts with no luck.

    Any ideas?

    Vito
    DemoDemo.com - Flash tutorials since 2002
    DemoWolf.com - 5,300+ Flash tutorials for hosting companies, incl. Voice tutorials

  2. #2
    Join Date
    May 2001
    Location
    Houston, TX
    Posts
    195
    You can try something like the following:

    http://javascript.internet.com/forms/word-counter.html

    There are more than likely better versions out there. I'll let you know if I run into anything.

  3. #3
    Join Date
    Jan 2002
    Location
    Toronto, Canada
    Posts
    11,059
    That's a good start. Thank you Eric. I do need a bit more functionality, though. I need it to do 2 things. First, I need it to do as it's doing now - providing a real time count for the user. But then I also need it to send me the results (text entered and word count) when the user clicks to submit the form.

    Vito
    DemoDemo.com - Flash tutorials since 2002
    DemoWolf.com - 5,300+ Flash tutorials for hosting companies, incl. Voice tutorials

  4. #4
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    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.
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  5. #5
    Join Date
    Jan 2002
    Location
    Toronto, Canada
    Posts
    11,059
    Wow, fyrestrtr, thanks for the elaborate reply.

    I'm painfully weak at coding, so it will take me a little while to work through your information. If I have any questions, I'll be sure to ask.

    One other point. I'm wondering if in the long run I should just pay someone to write a PHP script for this. In the final product, I need to have a form that is considerably more elaborate. This will be in an order page where the customer is buying a product that is priced based on the total word count. And it's not just one text field. The customer will have the option to populate up to 20 text boxes, and then the total word count of all the boxes together is what dictates the price.

    Is it worth trying to tweak an existing script or am I better off getting a new one written? And can all this be done exclusively in PHP? That would be my preference.

    Vito
    DemoDemo.com - Flash tutorials since 2002
    DemoWolf.com - 5,300+ Flash tutorials for hosting companies, incl. Voice tutorials

  6. #6
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    I would suggest that you do the final verification in PHP -- since that's the only reliable way to make sure your form isn't been misused. It is easy to do this in PHP. In fact, here is the code to do it :

    PHP Code:
    $total_words 0//Running total

    $ppw 2//price per word

    //A listing of all text areas that you want to calculate
    //With their respective limits (optional)
    //For example -- say you only want 10 words
    //for textboxA, 20 words for textboxB, and
    //textboxC has no limit on the words.

    $text_boxes = array('textboxA' => 10,
                                      
    'textboxB' => 20,
                                      
    'textboxC');

    //Here 'textboxA' is whatever the name= is of your textarea.
    //So, there should be something like <textarea name="textboxA">
    //in your form, etc.

    foreach($text_boxes as $name => $limit)
    {
        
    $word_count str_word_count($_POST["$name"]);
        if (
    $limit) {
        
    //There is a limit on this field
        
    if ($word_count $limit)
        {
            
    //Here the user exceeded the limit
            //for the given textarea
            
    $errors[] = "You have exceeded the ".$limit." word limit for ".$name;
        } else {  
    $total_words += $word_count; }
        } else { 
    $total_words += $word_count; }
    }

    //Now we have the total words, lets see if there were any errors

    if (is_array($errors))

       exit (
    "There were errors in your submission <br />".implode("<br />",$errors));
    }

    echo 
    "Your total is $".number_format(($ppw $total_word),2); 
    The display part is nice from the user's point of view. You can also opt to the the calculation on the fly (using javascript) -- so that before the user submits the form, they know how much the product is going to cost.

    All this sounds great -- but its bubkis if your user has javascript turned off (a few pedantic users do). Also, if your javascript gets extra fancy -- you might run into problems with cross browser compatibility.

    Let me know if you need more information/help.
    Last edited by Burhan; 06-25-2004 at 04:51 PM.
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  7. Newsletters

    Subscribe Now & Get The WHT Quick Start Guide!

Related Posts from theWHIR.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •