Skip to main content
Inspiring
November 15, 2010
Question

Syntax to correct typing errors on form

  • November 15, 2010
  • 9 replies
  • 2611 views

Hiya

Using PHP and MySQL, I am looking for the correct syntax to make sure a visitor has typed their name and address with an initial capital letter, then the rest of the address lowercase.

Finally the postcode (zip) should be uppercase.

I can correct any errors when it is DISPLAYED on screen, but what I want to do is to fix this BEFORE it is inserted into the database.

I have found endless entries on how to convert once the text is in the database but can't find anything about fixing it errors on the fly as the data is inserted into the database.

I want to export the database to a printer's mailing list so need all the addresses to be neat and tidy.

Thank you for any help!

This topic is closed to new replies. Start a new post to keep the conversation going.

9 replies

November 15, 2010

You would do this operation after the form is submitted using a server-side language like PHP or you could do it client side with Javascript. The latter has the drawback that the user may have disabled javascript, which would render the test moot.

Use the functions ucfirst() to make the innitial letter capital. If you have more than one name in the string, you will have to break up the string using explode() and then do this to each piece, and strtoupper() to uppercase the postal code. You should probably also perform other checks to make sure the post code conforms to regulation.

If you need documentation, www.w3schools.com/php is a good starting point.

Barry

Inspiring
November 16, 2010

Thanks for the reply. Although I was hoping there was someway I could just alter the INSERT script to something like this (which doesn't work)...

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "contactUsForm")) {

  $insertSQL = sprintf("INSERT INTO MailingList (Name, Phone, Email, Address1, Address2, Address3, Town, County, PostCode, MailOut) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",

                       GetSQLValueString ucwords($_POST['name'], "text"),

                       GetSQLValueString ucwords($_POST['phone'], "text"),

                       GetSQLValueString ucwords($_POST['email'], "text"),

                       GetSQLValueString ucwords($_POST['address1'], "text"),

                       GetSQLValueString ucwords($_POST['address2'], "text"),

                       GetSQLValueString ucwords($_POST['address3'], "text"),

                       GetSQLValueString ucwords($_POST['town'], "text"),

                       GetSQLValueString ucwords($_POST['county'], "text"),

                       GetSQLValueString strtoupper($_POST['postcode'], "text"),

                       GetSQLValueString($urlstore+1, "text"));

November 16, 2010

There is an SQL upper() function, but nothing to do initial caps.

You should be validating the data entry entry to avoid bad stuff from getting into the database, and to prevent hacking (SQL injection and the like). It's very easy at that point to do whatever conversion you need (in PHP). You will need to break up your NAME string if you want to have all parts of it with initial caps.

Barry