Need to limit the length of a field

He has: 9 posts

Joined: Sep 2004

Hi,

I'm a beginner at PHP/MySQL. I can access my database via PHPAdmin and I know just enough to view, delete, and edit records, and to backup the database but that's about it.

One of the fields in the db is a comment field that appears on the site as a text box and it currently allows users to enter as much text as they like but I want to limit this to about 30 characters. Can you give me an idiot's step by step guide to adding this limit to a text field?

TIA

chrishirst's picture

He has: 379 posts

Joined: Apr 2005

maxlength="30" ... />

W3Schools Input Tag

He has: 388 posts

Joined: Apr 2005

That is one way but any spamer could just take your code and remove it on their local machine.

but you can add this to your php code

$variable = $_POST['what-ever-the-textfield-name-is'];

$text = substr($variable,0,100);

then you can do what you want with the text variable eg

echo $text;

He has: 9 posts

Joined: Sep 2004

I spent some time on this yesterday and found the answer:

phpadmin, table, change the field to VARCHAR and then out a 30 character limit in.

Thanks for the tips.

He has: 388 posts

Joined: Apr 2005

Yes but your users could enter more than 30 characters then unknowingly lose half their post. I actuly think it would be best to use all 3 methods.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.