0

I have created a cloud page with fields like firstname, email, and one field as OTP. If someone submits the form I am storing all data in a data extension and creating a 4-digit random number using a random function and storing that into the OTP field. And sending that OTP number to the subscriber when someone registered. When the subscriber enters that OTP I need to cross-verify the data from the data extension and what he entered, when he enters the correct OTP I want to display a success or failure message. But when a subscriber enters OTP it was showing a 500 error message. Here is the code

HTML form when a user enters details.
<form action="**Form handler URL**" method="post">

            <h3><strong>Personal Informatiom</strong></h3>

            <p>

              Please enter your personal details.

            </p><br>

              <label>Email</label>

              <input type="email" required="required" placeholder="EMAIL" id="email" name="email">

              <label>First Name</label>

              <input type="text" required="required" placeholder="First Name" id="firstname" name="firstname">

            

            <button id="myButton" type="submit">Submit</button>

</form>
**File Handler page**
%%[    
    var @random, @num1, @num2

    set @num1 = 1000
    set @num2 = 9999

    set @random  = random(@num1, @num2)


    set @email = RequestParameter("email")
    set @firstname = RequestParameter("firstname")

    set @lookupdata = Lookup("TESTDE01","email","email",@email)

    if empty(@lookupdata) then

        set @data = insertde("TESTDE01","email", @email,"FirstName", @firstname, "OTP",@random)

        set @msg = "You have been succesfully registered"
]%%

%%=v(@msg)=%% Thank You<br><br>

<form action="%%=RequestParameter('PAGEURL')=%%" method="POST">
<label>Enter OTP Here</label>
<input type="text" required="required" placeholder="OTP" id="otpvalue" name="otpvalue"><br><br>
     <button id="myButton" type="submit">Submit</button>

</form><br><br>
OTP is %%=v(@random)=%%

%%[ else ]%%

%%[
    set @msgs = "You are already registered"

    endif
]%%

%%=v(msgs)=%% Thank you

%%[
    var @otpentered
    set @otpnumber = AttributeValue("OTP")
    set @otpentered = RequestParameter("otpvalue")

    set @otpde = Lookup("TESTDE01","OTP","email",@email)

    if @otpde == @otpvalue then

        set @result = "success"
    else
        set @result = "failure"
    endif

]%%
4
  • Where is the firstName input field? Jan 22, 2023 at 13:11
  • 1
    Please update your question to include a step-by-step of the flow from the email send. It's not clear how the subscriber arrives at the page or receives the OTP email. Jan 22, 2023 at 13:16
  • Sure @AdamSpriggs, Updated
    – SMS
    Jan 22, 2023 at 13:41
  • After you store and display the OTP, how does the subscriber get back to the page? Jan 23, 2023 at 13:17

2 Answers 2

0

When you use method POST in your form, the browser will send the data to the action page to process so basically it's a new page therefore your last code block will not work since it doesn't have old parameters and you would need to handle on the action page URL also.

In the code below, I put a submit check to see:

  • If they have not submitted yet, do the old logic.
  • If they have submitted, then check the OTP value from OTP parameter with the one in the DE to tell them they have summited with a correct one or not.

Then manage to get the current URL with parameters then leverage it to use in the action page.

Form handler page:

<script runat="server">
Platform.Load("core", "1.1.1");
     var requestHeader = Platform.Request.RequestURL;
     Variable.SetValue("@header",requestHeader);
</script>

%%[    
    var @random, @num1, @num2, @otpentered

    set @num1 = 1000
    set @num2 = 9999

    set @random  = random(@num1, @num2)


    set @email = RequestParameter("email")
    set @firstname = RequestParameter("firstname")
    set @otpvalue = RequestParameter("otpvalue")
    set @submit = RequestParameter("submit")

    
    set @otpde = Lookup("TESTDE01","OTP","email",@email)
    
    if empty(@submit) then
    
        set @submitParam = Concat('&submit=', 'done')
        set @formURL = Concat(@header, @submitParam)
        set @lookupdata = Lookup("TESTDE01","email","email",@email)
        

        if empty(@lookupdata) then

            set @data = insertde("TESTDE01","email", @email,"FirstName", @firstname, "OTP",@random)

            set @msg = "You have been succesfully registered"
]%%

%%=v(@msg)=%% Thank You<br><br>

<form action="%%=v(@formURL)=%%" method="POST">
<label>Enter OTP Here</label>
<input type="text" required="required" placeholder="OTP" id="otpvalue" name="otpvalue"><br><br>
     <button id="myButton" type="submit">Submit</button>

</form><br><br>
OTP is %%=v(@random)=%%

%%[ 
    else
        set @result = "You are already registered. Thank you."
        Output(Concat(@result))
    endif 
]%%

%%[
    else
        if @otpde == @otpvalue then
            set @result = "Success"
            Output(Concat(@result))
        else
            set @result = CONCAT("You submitted a wrong OTP! Your OTP is ", @otpde)
            Output(Concat(@result))
        endif
    endif
]%%
0

I think this whole thing can be simplified by leveraging a Triggered Send Data Extension to store the OTP, but if you'd like to keep a separate DE for storing the OTPs, you could do something like this:

%%[

  set @PageURL = RequestParameter('PAGEURL')

  /* send context */
  set @otp = AttributeValue("OTP")
  set @emailaddr = AttributeValue("emailaddr")

  /* form inputs */
  set @submitted = RequestParameter("submitted")
  set @email = RequestParameter("email")
  set @otpInput = RequestParameter("otpInput")
  set @firstName = RequestParameter("firstName")

  /* Triggered send */
  set @TSDKey = "YOURTRIGGERSEXTERNALKEY"

]%%

%%[ /* STEP 1: Subscriber arrives at page for the first time, submits email and name */ ]%%
%%[ if empty(@submitted) and empty(@otp) and not empty(@emailaddr) then ]%%

    <form action="%%=v(@PageURL)=%%" method="POST">
        <input name="submitted" type="hidden" value="true">
        <label>Email</label>
        <input type="email" required="required" placeholder="Email" id="email" name="email" value="%%=v(@emailaddr)=%%">
        <label>First Name</label>
        <input type="text" required="required" placeholder="First Name" id="firstName" name="firstName">
        <br><br><button id="myButton" type="submit">Submit</button>
    </form>

%%[ /* STEP 2: subscriber has submitted name and email, create OTP and store it */ ]%%
%%[ elseif @submitted = "true" and not empty(@email) and not empty(@firstName) then ]%%

    %%[

        set @num1 = 1000
        set @num2 = 9999
        set @OTP  = random(@num1, @num2)
        set @lookupEmail = Lookup("TESTDE01","email","email", @email)

        if empty(@lookupEmail) then
           
            InsertDE("TESTDE01","email", @email,"FirstName", @firstname, "OTP", @OTP)

            /* Trigger an email to send OTP to recipient */
            set @ts = CreateObject("TriggeredSend")
            set @tsDef = CreateObject("TriggeredSendDefinition")

            /* Set the External Key of the Trigger Send Definition */
            SetObjectProperty(@tsDef, "CustomerKey", @TSDKey)
            SetObjectProperty(@ts, "TriggeredSendDefinition", @tsDef)

            /* Create the Subscriber Object */
            set @ts_sub = CreateObject("Subscriber")
            SetObjectProperty(@ts_sub, "EmailAddress", @email)

            /* Set SubscriberKey to EmailAddress */
            SetObjectProperty(@ts_sub, "SubscriberKey", @email)

            /* Create and Set Attributes */
            set @attr = CreateObject("Attribute")
            SetObjectProperty(@attr, "Name", "OTP")
            SetObjectProperty(@attr, "Value", @OTP)
            AddObjectArrayItem(@ts_sub, "Attributes", @attr)

            set @attr = CreateObject("Attribute")
            SetObjectProperty(@attr, "Name", "FirstName")
            SetObjectProperty(@attr, "Value", @FirstName)
            AddObjectArrayItem(@ts_sub, "Attributes", @attr)

            /* Add all Attributes into Array */
            AddObjectArrayItem(@ts, "Subscribers", @ts_sub)

            /* Execute the trigger send */
            set @ts_statusCode = InvokeCreate(@ts, @ts_statusMsg, @errorCode)

            /* RaiseError if Trigger fails */
            if @ts_statuscode != "OK" then
                raiseError(@ts_statusmsg, 0, @ts_statuscode, @errorcode)
            endif

            output(concat("<br>You have been successfully registered.  Thank You."))
            output(concat("<br><br>Your OTP is: ", @OTP))

          
        else

            output(concat("<br>You are already registered.  Thank You."))

        endif

    ]%%


%%[ /* STEP 3: subscriber arrives at page from an email with the OTP in the context */ ]%%
%%[ elseif empty(@submitted) and not empty(@otp) and not empty(@emailAddr) then ]%%


    <form action="%%=v(@PageURL)=%%" method="POST">
        <input name="submitted" type="hidden" value="true">
        <input name="emailAddr" type="hidden" value="%%=v(@emailaddr)=%%">
        <label>Enter OTP Here</label>
        <input type="text" required="required" placeholder="OTP" id="otpInput" name="otpInput">
        <br><br><button id="myButton" type="submit">Submit</button>
    </form>  


%%[ /* STEP 4: subscriber has entered the OTP, check it */ ]%%
%%[ elseif not empty(@submitted) and not empty(@otpInput) and not empty(@emailAddr) then ]%%

    %%[

        set @otpLookup = Lookup("TESTDE01","OTP", "email", @emailaddr)

        if @otpLookup == @otpInput then
            output(concat("<br>Verified."))
        else
            output(concat("<br>Invalid OTP."))
        endif

    ]%%

%%[ /* invalid value for submitted*/ ]%%
%%[ else ]%%

    <br>Bad request

%%[ endif ]%%

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .