+5  A: 

It can be done. All you need to do is create a couple of hidden input elments (eg <input type="hidden" name="characterCount" /> and put the values in there when you have calculated them. However you SHOULDN'T do this (hence no code - I don't wnat to encourage you).

You should always calculate anything critical like this on the server anyway since in theory a user can post whatever they want so they could alter their request to say "credits=0" and you don't want to trust that. Or if they fiddle the character count then presumably somethign will crash later on because it receives input that is too long... Or maybe they told you it had a negative number of characters... Just revalidate on server. Its the way to go. :)

Chris
+1 for security
Ryan Kinal
+1 for pointing out the huge flaw. And why would a user override and send `credits=0`. If anything, they will say, `credits=999999..` :)
Anurag
@Anurag: Its how many credits will be used to send the message. Its like a price so you just went bankrupt. ;-)
Chris
@Chris - I thought that was the remaining credits the user had. I mean when we're sending all this information via JavaScript, then why can't remaining credits come from a *hidden* field.
Anurag
@Anurag: I'm not going to worry about it. We've helped the OP learn the right way to do the big things. Hopefully he won't be passing any credit information to the server so its all irrelevant really. ;-)
Chris
A: 

You can make a couple hidden fields and put the counts there as well as in the spans and then submit them with the form. However, this system can be easily gamed by forging the http request with fake character and credit count, so you may want to still do the counting on the server to avoid fraud. Also if the user has javascript turned off the count will be zero.

To do this where you now have

$chars.text(len);

add

$("#my_hidden_char_count").val(len);

which assumes you have

<input id="my_hidden_char_count" value="0"></input>

in your form. Do the same for credits.

Adam