Count textareas with data

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Jude
Forum Newbie
Posts: 2
Joined: Thu Jun 19, 2008 11:27 am

Count textareas with data

Post by Jude »

I have a form with twenty textareas. I need to check the textareas for data. If ten of the twenty textareas have data then the user can post to the database. If less then ten are filled in then the user needs to get an error message "You must fill out at least 10 of the 20 following requirements for Green School consideration."

I am inexperienced in php, and would appreciate any help.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Count textareas with data

Post by Benjamin »

Code: Select all

 
$tna = array('text_area_one', 'text_area_two', 'text_area_three'); # you can add the rest
$cnt = 0;
 
foreach ($tna as $ta)
{
    if (!empty($_POST[$ta]) && strlen($_POST[$ta]) > 10)
    {
        $cnt++;
    }
}
 
if ($cnt < 10)
{
    # dude didn't fill out at least 10 text areas
}
 
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Count textareas with data

Post by califdon »

First, PLEASE READ THE FORUM RULES: viewtopic.php?f=6&t=30037

Second, please notice THE FIRST FORUM RULE:
Forum Rules wrote:1. Forum Rules
1.1 Posting
1. Select the correct board for your query. Take some time to read the guidelines in the sticky topic.
You posted this in PHPDN Suggestions. I have moved it to Client Side.

The reason I moved it to Client Side is that you surely don't want to send a new page back to the browser when you could much more easily alert the user to finish the form, using Javascript, a client side language. The one reason for not using Javascript is that something like 5% (the latest figures I've seen) of users opt to disable Javascript in their browsers. If this is a real concern, then you would have to rely on sending a new page back to the browser, but in most cases I wouldn't recommend doing that.

In Javascript, you can write a function to count the number of textareas that have no entry and call that function in the onClick event of the Submit button. Perhaps, now that this is in the appropriate Forum, someone may suggest details to do this.
Jude
Forum Newbie
Posts: 2
Joined: Thu Jun 19, 2008 11:27 am

Re: Count textareas with data

Post by Jude »

I keep getting a Parse error: parse error, unexpected $ in /var/www/kisd/Green_School/checklistOld.php on line 578

Code: Select all

$tna = array('recycling_text1', 'recycling_text2', 'recycling_text3', 'recycling_text4','recycling_text5','energy_text1','energy_text2','energy_text3','energy_text4','energy_text5','environ_text1','environ_text2','environ_text3','environ_text4','environ_text5','environ_text6','environ_text','environ_text','environ_text9','environ_text10'); 
 
 $cnt = 0;
  
 foreach ($tna as $ta)
 {
     if (!empty($_POST[$ta]) && strlen($_POST[$ta]) > 10)
     {
         $cnt++;
     }
 }
  
 if ($cnt < 10)
 {
 echo "You must reply to at least ten of the text requirements before you submit Finished.";
 
 }
else
 {
    $mailBody = "A Green School Checklist has been completed" . "\n\n";
    $mailBody .= "Name: "  . $_POST['fname'] . " " . $_POST['lname'] . "\n";
    $mailBody .= "School: " . $_POST['school'] . " " . "Application Date:" . $_POST['appdate'] . "\n";
   
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Count textareas with data

Post by RobertGonzalez »

What is on line 578 and the lines just before it?
Post Reply