text box counter without using javascript

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

text box counter without using javascript

Post by ninethousandfeet »

hello,

i've done some searching and it seems like every text box/area counter requires the use of javascript. does anyone know if this can be done without using javascript? i would like to make my site as compatible as possible.

thank you!
MasterBeta
Forum Commoner
Posts: 38
Joined: Thu Apr 02, 2009 4:35 am
Location: Lincoln, NE

Re: text box counter without using javascript

Post by MasterBeta »

Your post is a little too vague on what your counting. You could be counting characters typed into a text field or how many times a form is submitted, but without more info, I'm not sure anyone can help.
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: text box counter without using javascript

Post by ninethousandfeet »

sorry about that... i am looking to create a text box/area counter that counts down the characters entered as the user is typing... without using javascript.
any suggestions? everything i've seen leads to javascript and i would like to see if there is another way to do it.
MasterBeta
Forum Commoner
Posts: 38
Joined: Thu Apr 02, 2009 4:35 am
Location: Lincoln, NE

Re: text box counter without using javascript

Post by MasterBeta »

I'm not sure why you don't want to use javascript but PHP will not do this, since it's a server side language. You need to use a client side programming language like javascript. Once a page is called using PHP, your interactions are not sent to the server until you submit the form. You could use Flash, but I'd recommend javascript.

I guess you could validate it if that would help, but the form must be submitted in order to do this.

Code: Select all

<?php
$maxChars = 5; // Set the maximum characters allowed
 
if(isset($_POST['textarea'])){ // check if form has been submitted
    $chars = strlen($_POST['textarea']); // count the characters in the form field textarea
    if($chars > $maxChars) $error = "Please limit your text to ". $maxChars. " characters."; // if greater assign an error to print
}
print $error; // print the error message
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="myForm" id="myForm">
  Max Length = 5 characters<br />
  <textarea name="textarea" id="textarea" cols="50" rows="10"><?php echo $_POST['textarea']; ?></textarea>
  <br />
  <input name="Submit" type="submit" value="Submit" />
</form> 
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: text box counter without using javascript

Post by ninethousandfeet »

i wanted to know if there was a way around using javascript in case users had disabled javascript on their browser, but it doesn't look like it. thank you very much. i think i'll use the javascript and the validation and that will have to be enough. thanks again.
Post Reply