Can someone point me to some coding examples that show how to use ampscript to make a cloudpage form that will write to a data extension. I've been in the trailhead but it doesn't get detailed enough. I've tried Smart Capture forms but they don't have all the functionality I need - like checking to see if user is already in the DE and, if so, throwing out an error message.
Take a look at the Ampscript.Guide website for some solid examples of doing this function within a CloudPage. Ultimately, the ampscript function you'll be using is one of these:
SmartCapture is pretty limited in function & style, in my experience.
Adding to all the above comments and answers.
Don't forget page security and data cleanliness. For security, trailhead actually has some very useful resources:
Follow standard best practices:
Don't let people gather information from your database on public webforms. Example: Do not auto-populate forms with data from any Data Extension once the user enter their email address in an attempt "to be more user-friendly". If I enter your email address, will I see your data?
Don't send unencrypted personal data in GET requests (commonly known as "normal links"). Use POST Requests where possible (between form and handler). From emails, only GET Requests are possible, so point to your cloudpage with CloudPagesURL
- Before the next two steps, stop and think: What data format does your database expect?
Are you writing into any old data extension? Is this a process you want to repeat? Does the data have to align with other data in other DEs or other databases? Example: Are you entering the data into Sales Cloud? What's the character limit for e.g. "firstname" in Sales Cloud? Are you enforcing this? A more complex example: How do phone numbers have to be structured? Ensure you meet these demands, try not to create a mess with new logics everytime someone comes up with a webform.
Backend validate for security. As a general best practice, make sure the data input is actually formatted as expected before you process it further.
You can use RegexMatch (which leverages Regular Expressions/ RegEx) for this. here is an example for validating an email address using RegEx:
Assume a data extension "RegexPatterns" with the following fields. Note that both the regex for frontend validation (using HTML5) and AMPScript backend validation are in here. The formpage looks up the patternHTML5, the handlerPage looks up the patternAMPscript. The format of those two differs slightly (AMPScript needs leading ^ and trailing $)but it makes sense to have both in one place. For demonstration purposes, I used the HTML5 standard for email formatting. This might deviate from what you actually need.
This can get you started and can be enhanced further. It would be code on your handler page that is done before any processing occurs:
%%[SET @Email = RequestParameter("email") SET @patternForEmail = Lookup("ENT.RegexPatterns","PatternAMPScript","Fieldname","Email") /* not empty check: only non-empty input is validated with the Regex. A failed RegEx validation check results in @valid_Email being set to EMPTY. no user input results in @valid_Email being set to "no_userinput". */ IF NOT EMPTY(@email) THEN SET @valid_Email = RegExMatch(@email, @patternForEmail, 0) ELSE SET @valid_Email = "no_userinput" ENDIF /* Logical test on the result of the string validation: If the regex was successfully passed and the input field was not left empty, @regexCheckvalid is set to true. This is used to determine whether processing continues or aborts. This process can be used for MANDATORY fields. If no user input should be a valid entry, then change the next IF Statement to: IF not empty(@valid_Email) THEN ... */ IF not empty(@valid_Email) and @valid_Email != "no_userinput" THEN SET @regexCheckValid = true ENDIF IF @regexCheckValid == true THEN /* continue processing */ ELSE /* validation error. display error message */ ENDIF ]%%