Ad
Ad
Ad
Amp Script

Read Values from Form and insert/update into DataExtension using Ampscript

Pinterest LinkedIn Tumblr

In this article, we will try to understand the basics of AmpScript and insert records into Data Extension. In our previous article, we learned how to personalize using Ampscript. Ampscript code starts with %% and ends with %%. For example, if we wanted to get the dynamic value of Email from Data extension we can use %%Email%% on the Email template or on the cloud page. In case of Cloud page if we want to write an ampScript block then it starts with %%[ and ends with ]%%.

%%[  

/** Start of Variable Declarations **/

var @newName,@newEmail,@newPhone ,@website,@Submit

/** End of Variable Declarations **/

/** Start of Setting Dynamic/static values to desired variables **/

set @newName = RequestParameter(“name”)

set @newEmail = RequestParameter(“email”)

set @newPhone = RequestParameter(“phone”)

set @website = ‘Home Page’

set @Submit = RequestParameter(‘submit’)

/** End of Setting Dynamic/static values to desired variables **/

/** Validate if the variable has got the desired value before inserting into Data extension **/

If @submit == ‘Submit’ Then

InsertDE(“Customer_Form”,”Email”, @emailAddress, “Name”, @newName,”Phone”,@newPhone)

Endif

]%%

<label>Name:</label> <input name=”name” type=”text” value=”%%Name%%” />;

<label>Email:</label> <input name=”email” type=”text” value=”%%email%%” />

<label>Phone:</label> <input name=”phone” type=”number” value=”%%phone%%” />

<input name=”submit” type=”submit” value=”Submit” />

In the above code snippet, we have started our ampscript code with %%[ and then we declared variables using var keyword. Every variable must be declared/starts with @. We have declared newName, newEmail, newPhone for demo purposes.

var @newName,@newEmail,@newPhone,@website,@Submit

After declaring the variables the next step is to populate the variables with some values. We can set any value to the variable as it can hold text or numbers. In our scenario, we want to collect all the dynamic values such as name, email, phone and submit from the form which the customer has entered.  RequestParameter function in Ampscript helps in getting dynamic values from the form.

set @newName = RequestParameter(“name”)

set @newEmail = RequestParameter(“email”)

set @newPhone = RequestParameter(“phone”)

set @website = ‘Home Page’

set @Submit = RequestParameter(‘submit’)

After the Declaration of variables and setting/getting the values into variables, our next step is to use the variables and send it to data extension.

We always have to do conditional based logic to avoid unnecessary calls/errors to data extensions. To implement conditional logic we can use If condition/If else condition similar to other programming/Scripting languages. The syntax is a little different when compared to other programming/Scripting languages.

If (condition is true) Then

do this

Endif

In our scenario, we are checking if @submit variable is populated with value as submit then insert into data extension

If @submit == ‘Submit’ Then

InsertDE(“Customer_Form”,”Email”, @emailAddress, “Name”, @newName,”Phone”,@newPhone)

Endif

InserDE syntax helps in inserting data into Data Extension. The first parameter takes the data extension name and is a required field. We also need a column name on the Data extension for which the value has to be inserted.

Syntax

InsertDE(1, 2, 3)

OrdinalTypeDescription
1stringRequiredName of the data extension from which to insert the specified row
2stringRequiredColumn name used to build insert clause
3stringRequiredColumn value used to build insert clause

Usage

InsertDE(‘SomeDE’,’FirstName’,FirstName, ‘LastName’,LastName, ‘CreatedDate’,NOW())

Write A Comment