0

I have a quick question, I would like to pass values from generated input using javascript to an ampscript variable. My goal is to create a data extension MC from a Landing Page so the user can insert the name of the DE and the number of column he wants and then in function of this number, inputs will be generated for the user to insert the name of each column in these input.

I tested my code but I can't get it work, I can't pass the value inserted in the input to an ampscript variable, below is my code :

PageID : 10631 is the actual page where the form is so when the customer click on submit, he will be redirected to the same page where the Ampscript code is located.

The form :

  <form action="%%=RedirectTo(CloudPagesURL(10631))=%%" method="get">

    <input type='text' placeholder='Name' name='nameDE'></input>
    <input type="number"placeholder='Column Number' name="nombrePlat" id="nombreP" >
    <br>
    <input id ="p2" type="button" value="OK" onclick="getP()">
    <div   id="inutContainer" ></div>
    <br>
    <input id="submitted" type="hidden" name="submitted" value="submitted" />

    <input type="submit" onclick="getvalu2()">

getvalu2() is the javascript function I created to retrieve the value :

function getvalu2() { 

var x = +document.getElementById("form1").value;

Variable.SetValue("@x",x);
alert(x); }

Below is how I generate my input using javascript, so the user, enter 2 (for example) and two input will be generated in the form :

function getP() {
var nbP = +document.getElementById("nombreP").value;
var inputContainer = document.getElementById("inutContainer");

for (var i = 1; i < nbP+1; i++) {
    var newForm = document.createElement("input");
    newForm.setAttribute("type", "text");
    newForm.setAttribute("placeholder", "Name Column "+i)
    newForm.setAttribute("id", "form"+i);
    inputContainer.appendChild(newForm);
    inputContainer.appendChild(document.createElement("br"));
} document.getElementById("p2").style.display="none"; }

Below is a sample of the Ampscript of the page, the name of the DE is well retrieved because it's not generated with javascript (not all the ampscript code is there but the part that doesn't work is) :

    %%[

  var @x

  if RequestParameter("submitted") == "submitted" then

  set @NameDE = RequestParameter('nameDE')
  set @x = RequestParameter('x')

/*CREATE THE DATA EXTENSION*/
Set @de = CreateObject("DataExtension")
SetObjectProperty(@de,"Name",@NameDE)
SetObjectProperty(@de,"CustomerKey",@NameDE)
SetObjectProperty(@de,"Description","Data Extension Created via API")
SetObjectProperty(@de,"IsSendable","False")
SetObjectProperty(@de,"IsTestable","False")

Set @deFields = CreateObject("DataExtensionField")
SetObjectProperty(@deFields,"FieldType","Text")
SetObjectProperty(@deFields,"IsRequired","false")
SetObjectProperty(@deFields,"IsPrimaryKey","false")
SetObjectProperty(@deFields,"IsNillable","false")
SetObjectProperty(@deFields,"MaxLength","50")
SetObjectProperty(@deFields,"Name",@x)
AddObjectArrayItem(@de,"Fields",@deFields)

 /* This field sets how the data extension field above relates to the all subscribers list */
SET @ts_attr = CreateObject("Attribute")
SetObjectProperty(@ts_attr, "Name", "Email Address")
SetObjectProperty(@ts_attr, "Value", "Email Address")
SetObjectProperty(@de,"SendableSubscriberField",@ts_attr)

Set @StatCode = InvokeCreate(@de, @StatMessage, @ErrorCode)

endif

  ]%%

Is anything missing here that could fixed the issue?

0

I got it working !

I added this to my javascript :

newForm.setAttribute("name", "form"+i);

to my javascript that generated the input and this to the ampscript :

set @x = RequestParameter('form1')

Thanks again !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.