lc21 Posted July 14, 2007 Share Posted July 14, 2007 I have managed to create a script in javascript to ensure that the user can only enter a certain amount of characters in a text area. If users have javascript turned off how can I check for this in PHP? Thank you. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 14, 2007 Share Posted July 14, 2007 Use the strlen() function. Quote Link to comment Share on other sites More sharing options...
lc21 Posted July 14, 2007 Author Share Posted July 14, 2007 Thanks, how do I get the string stored in a variable though as the text area just has a name can I use that name and set it to a variable then use the strlen function? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 14, 2007 Share Posted July 14, 2007 Yep. If your method is post and the name of the text area was textarea: <?php if(strlen($_POST['textarea']) > 100){ //too long }else{ //length ok } ?> Quote Link to comment Share on other sites More sharing options...
182x Posted July 14, 2007 Share Posted July 14, 2007 Good tip could come in handy. Quote Link to comment Share on other sites More sharing options...
lc21 Posted July 14, 2007 Author Share Posted July 14, 2007 When the data from a text area is stored in a database does it loose all the white space or when outputting it again is there a way to retain this? Quote Link to comment Share on other sites More sharing options...
drewbee Posted July 14, 2007 Share Posted July 14, 2007 I always recomend doing a trim() of the variable to clean off any additional whitespace that may exist. <?php if (strlen(trim($_POST['myVar'])) > 0) { // Do it!! } else { // Please enter a value } ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 15, 2007 Share Posted July 15, 2007 When the data from a text area is stored in a database does it loose all the white space or when outputting it again is there a way to retain this? Use the nl2br() function when you output data that came from a text box. It converts all new lines(\n) to html <br /> tags. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.