RobertSubnet Posted March 20, 2008 Share Posted March 20, 2008 Greetings all. Does anyone have a link for where I can find/download a textbox character counter? I do not want anything in Java (found plenty of those). I would prefer something dynamic, that is as you type the counter increases automatically. But something where you "click to count" will also do. Thank you for your help. ~Robert Link to comment https://forums.phpfreaks.com/topic/97012-character-counter/ Share on other sites More sharing options...
RobertSubnet Posted March 20, 2008 Author Share Posted March 20, 2008 <Bueller?> Link to comment https://forums.phpfreaks.com/topic/97012-character-counter/#findComment-496566 Share on other sites More sharing options...
thebadbad Posted March 20, 2008 Share Posted March 20, 2008 If you want a dynamic counter, the only way to go is Javascript. And the 'click to count' solution will require you to submit the form, and let PHP count the chars. You could do like this: <?php if ($_POST['submit'] == 'count') { echo 'The textarea contains ', strlen($_POST['countthis']), ' characters.<br />'; // and now we fill in what the user already typed in the fields below } elseif ($_POST['submit'] == 'submit') { // code to run when the form is submitted } ?> <form action="" method="post"> <input type="text" name="firstfield" value="<?php echo $_POST['firstfield']; ?>" /> <input type="test" name="secondfield" value="<?php echo $_POST['secondfield']; ?>" /> <textarea name="countthis"><?php echo $_POST['countthis']; ?></textarea> <input type="submit" name="submit" value="count" /> <input type="test" name="thirdfield" value="<?php echo $_POST['thirdfield']; ?>" /> <input type="submit" name="submit" value="submit" /> </form> Link to comment https://forums.phpfreaks.com/topic/97012-character-counter/#findComment-496601 Share on other sites More sharing options...
thebadbad Posted March 20, 2008 Share Posted March 20, 2008 And if you're looking to write any HTML in the fields, you should run a htmlspecialchars() when echoing them in the fields, and then a 'unhtmlspecialchars' when e.g. inserting them into a database on submit (and before you count the characters in the textarea). <?php function unhtmlspecialchars($string) { $string = str_replace('&', '&', $string); $string = str_replace('"', '"', $string); $string = str_replace('<', '<', $string); $string = str_replace('>', '>', $string); return $string; } ?> Else writing something like </textarea> into the textarea will break it, and that part will be gone after a count. Link to comment https://forums.phpfreaks.com/topic/97012-character-counter/#findComment-496606 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.