Page 1 of 1
text box counter without using javascript
Posted: Wed Apr 01, 2009 6:17 pm
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!
Re: text box counter without using javascript
Posted: Thu Apr 02, 2009 5:50 am
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.
Re: text box counter without using javascript
Posted: Thu Apr 02, 2009 2:53 pm
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.
Re: text box counter without using javascript
Posted: Fri Apr 03, 2009 4:17 am
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>
Re: text box counter without using javascript
Posted: Fri Apr 03, 2009 3:07 pm
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.