1

I'm trying to create a variable that will be true or false based on a checkbox on another page. I'm using RequestParameter to get the information from the checkbox but having some trouble. Here is my current AMPscript. Any help anyone has would be appreciated thanks!

def variable

%%[
SET @variable = RequestParameter("checkbox") 

IF @variable == null THEN
        SET @boolean = "False"

ELSEIF @variable != null THEN
        SET @boolean = "True"

ENDIF
]%%

5 Answers 5

3

I'd suggest displaying the values manually or with the output() to debug:

%%[

SET @variable = RequestParameter("checkbox") 

output(concat("<br>variable: ", @variable))

IF @variable == null THEN

    SET @boolean = "False"

ELSEIF @variable != null THEN

    SET @boolean = "True"

ENDIF

output(concat("<br>boolean: ", @boolean))

]%%

<br>variable: %%=v(@variable)=%%
<br>boolean : %%=v(@boolean)=%%

Also, you should check out the empty() function. I think you'll find checking for null won't work.

2
  • Thanks Adam! Now my only concern is how to know if the user has changed the checkbox. Is there an AMPscript function that will tell me what the current status of a checkbox is?
    – okc
    Commented Aug 17, 2016 at 19:57
  • 1
    Keep in mind that AMPScript is server-side. It is evaluated before the page loads. Whatever checking you'll need to do will need to be in the form of a form post or AJAX call. Commented Aug 18, 2016 at 2:09
1

I've previously had issues using checkboxes in AMPscript, I used the following solution to fix my issues:

IF RequestParameter("IA_Weekly_Rates_Box") == "true" THEN
    SET @IA_Weekly_Rates_Box = "true"
ELSE
    SET @IA_Weekly_Rates_Box = "false"
ENDIF  

Then i'd use javascript to explicitly set the values of the checkbox when a user checks or unchecks.

1
  • Thanks! That ended up being about what I had to do. Glad to hear others have come to the same conclusion.
    – okc
    Commented Aug 22, 2016 at 21:09
0

I know this is old but I came across this question while trying to fix an issue of my own. Supplying my code in hopes it helps someone else some day.

IF RequestParameter('subscribe') == "on" THEN
  SET @subscribe = "true"
ELSE
  SET @subscribe = "false"
ENDIF  

<input id="subscribe" name="subscribe" type="checkbox">

No javascript necessary.

0

I know this is old as heck, but check this:

%%[SET @Monthly= RequestParameter('Monthly')]%%
<h1>Available Publications:</h1>
    <label>Monthly: <input id="monthly" name="Monthly" type="checkbox" %%=IIF(@Monthly, "checked", "")=%%> </label>
%%[SET @pub1 = RequestParameter('Monthly')
  If @pub1 == 'on' THEN
     SET @Monthly  = 'true'
     ELSE
     SET @Monthly  = 'false'
     ENDIF]%%

Then you'll be all set with boolean values. Figuring out that check boxes use "on" or "off" was a pain haha

0

We did it like this:

%%[
VAR @Topic1
SET @Topic1 = RequestParameter("Topic1")
]%%

<input name="Topic1" type="checkbox" value="true" %%=IIF(@Topic1==true, 'checked', '')=%%>&nbsp;&nbsp;Topic1</label>

You must log in to answer this question.

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