Jump to content

Character counter


RobertSubnet

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.