Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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"));
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
colinwalton wrote:
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)...
It would - if you did it correctly. ![]()
Instead of this:
GetSQLValueString ucwords($_POST['name'], "text"),
It should be this:
GetSQLValueString (ucwords($_POST['name'], "text")),
Copy link to clipboard
Copied
Hoorah! I knew if my hero David Powers was on the case there would be a simple answer.
Thanks - just what I was looking for.
Colin
Copy link to clipboard
Copied
Well it looks like i got too excited.
Now I have tried it I get a string of errors I don't understand:
Warning: Wrong parameter count for ucwords() in /content/HostingPlus/m/a/maisonhomeinteriors.com/web/JoinOurMailingList.php on line 43
Warning: Missing argument 2 for GetSQLValueString(), called in /content/HostingPlus/m/a/maisonhomeinteriors.com/web/JoinOurMailingList.php on line 43 and defined in/content/HostingPlus/m/a/maisonhomeinteriors.com/web/JoinOurMailingList.php on line 9
Warning: Wrong parameter count for strtolower() in /content/HostingPlus/m/a/maisonhomeinteriors.com/web/JoinOurMailingList.php on line 45
Warning: Missing argument 2 for GetSQLValueString(), called in /content/HostingPlus/m/a/maisonhomeinteriors.com/web/JoinOurMailingList.php on line 45 and defined in/content/HostingPlus/m/a/maisonhomeinteriors.com/web/JoinOurMailingList.php on line 9
Warning: Wrong parameter count for ucwords() in /content/HostingPlus/m/a/maisonhomeinteriors.com/web/JoinOurMailingList.php on line 46
etc etc....
Is there something obvious I am not doing?
Copy link to clipboard
Copied
colinwalton wrote:
Is there something obvious I am not doing?
Yes and no.
First, the no: there's a mistake in the code I gave you. I posted the reply without testing it, but the error messages tell me immediately what's wrong. It should be this:
GetSQLValueString((ucwords($_POST['name']), "text")),
The yes part of my answer is that you're not reading (or at least understanding) the error messages. The first one tells you that ucwords() has the wrong parameter count - in other words, the wrong number of arguments. The second one is a bit easier to understand, because it says there's a missing argument for GetSQLValueString().
All this boils down to the fact that the two functions were incorrectly nested. The way that PHP works is similar to mathematics: the innermost set of parentheses is processed first. So, ucwords($_POST['name']) is processed first, changing the initial letters to uppercase. The result is then used as the first argument to GetSQLValueString(), which makes the user input safe to insert into a SQL query.
Because you want to make sure the user input has uppercase first letters, but the rest in lowercase, it might be a good idea to do this instead:
GetSQLValueString((ucwords(strtolower($_POST['name'])), "text")),
That converts the input to lowercase first, then turns the first letters to uppercase, and finally passes the result to GetSQLValueString().
Copy link to clipboard
Copied
Great thanks - I learn something new everyday!
I tried what you suggested but Dreamweaver came up with syntax errors so I thought perhaps there was no need for extra pair of brackets. So the final answer that works perfectly is as below.
GetSQLValueString(ucwords(strtolower($_POST['name'])), "text"),
GetSQLValueString($_POST['phone'], "text"),
GetSQLValueString((strtolower($_POST['email'])), "text"),
GetSQLValueString(ucwords(strtolower($_POST['address1'])), "text"),
GetSQLValueString(ucwords(strtolower($_POST['address2'])), "text"),
GetSQLValueString(ucwords(strtolower($_POST['address3'])), "text"),
GetSQLValueString(ucwords(strtolower($_POST['town'])), "text"),
GetSQLValueString(ucwords(strtolower($_POST['county'])), "text"),
GetSQLValueString((strtoupper($_POST['postcode'])), "text"),
GetSQLValueString($urlstore+1, "text"));
Thank again David, for pointing me in the right direction. C
Copy link to clipboard
Copied
Great stuff!!
Worked for me. I didn't use lowercase for the rest, as it is name and address fields. However, location of parantheses made the difference.
Thank you.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more