NetTalk Central

Author Topic: GetValue of Multiple Checkbox Array  (Read 2104 times)

trent

  • Full Member
  • ***
  • Posts: 204
    • View Profile
    • Email
GetValue of Multiple Checkbox Array
« on: November 12, 2013, 03:18:36 PM »
Hi Everyone,

I have an HTML Form that is built by an HTML builder. The Form can have multiple checkboxes in a single fieldset that I need to get the values of. Here is an example of 3x checkboxes in a fieldset:

<input type="checkbox" value="answer 1" id="LMQM9" name="LMQ7[]">
<input type="checkbox" value="answer 2" id="LMQM10" name="LMQ7[]">
<input type="checkbox" value="answer 3" id="LMQM11" name="LMQ7[]">

How can I get the values of the checked checkboxes in the LMQ7[] array similar to this in PHP:

<?php
if(!empty($_POST['LMQ7'])) {
    foreach($_POST['LMQ7'] as $check) {
            echo $check;
    }
}
?>

I have tried putting the values into a local queue but my debug shows the GetValue result is empty:

free(MultipleChoiceQ)
LOOP
  LOC:QuestMCAnswer = p_web.GetValue('LMQ'& LMQ:SysID)
  debug('LOC:QuestMCAnswer = '& clip(LOC:QuestMCAnswer))
  if errorcode() then break.
  if LOC:QuestMCAnswer = '' then break.
  MCQ:Answer = clip(LOC:QuestMCAnswer)
  add(MultipleChoiceQ)
end!loop

Regards,
Trent

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11192
    • View Profile
Re: GetValue of Multiple Checkbox Array
« Reply #1 on: November 13, 2013, 06:20:03 AM »
what does the incoming POST statement in the log look like?

cheers
Bruce

trent

  • Full Member
  • ***
  • Posts: 204
    • View Profile
    • Email
Re: GetValue of Multiple Checkbox Array
« Reply #2 on: November 13, 2013, 12:12:32 PM »
Hi Bruce,

The incoming POST can look like this:

POST /courses/preview/testCoursePage?cid=6&uid=4&tid=4&pid=5 HTTP/1.1
Host: removed:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Referer: http://removed:8080/courses/preview/Page5.HTML
Cookie: SESSIONID=cUiKruWxNRY7sSdOB8B93zsXE6036Z
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 120

LMQ2=True&LMQ3=an+answer&LMR6=test&LMQ7%5B%5D=answer+1&LMQ7%5B%5D=a+short+answer&LMR8=&LMR9=&LMR10=&LMR11=&LMR12=&LMR13=

I can see that it's putting '%5B%5D' after the element name 'LMQ7'. A google search says that %5B = [ and %5D = ].

Changed my local queue code to the below and now I get the value of the very last checked checkbox but no other checked checkbox values:

free(MultipleChoiceQ)
LOOP
  LOC:QuestMCAnswer = p_web.GetValue('LMQ'& LMQ:SysID &'[]')
...

How can I get the other values?

Regards,
Trent

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11192
    • View Profile
Re: GetValue of Multiple Checkbox Array
« Reply #3 on: November 18, 2013, 12:33:07 AM »
I have added some code to the parser in 7.29.
Basically, if you receive variables as arrays, then there are a couple ways they could come in.

If they come in as un-indexed values, then they'll be allocated to an index as they arrive. So your example of

LMQ7[]=answer+1&LMQ7[]=a+short+answer

would be accessible as

p_web.GetValue('LMQ7[1]')
and
p_web.GetValue('LMQ7[2]')
respectively.

An additional value, LMQ7_maximum_ is also created for you. (In this case it holds the value 2).
So
p_web.GetValue('LMQ7_maximum_')
returns the number of elements in the array.

If they come through as indexed values, then they're just stored "as is".
eg
LMQ7[1]=answer+1&LMQ7[2]=a+short+answer

would be accessible as
p_web.GetValue('LMQ7[1]')
and
p_web.GetValue('LMQ7[2]')
respectively.

The _maximum_ value is also set for you in this case (but obviously not all values up to the maximum are necessarily included in the POST)

trent

  • Full Member
  • ***
  • Posts: 204
    • View Profile
    • Email
Re: GetValue of Multiple Checkbox Array
« Reply #4 on: November 18, 2013, 12:37:08 PM »
Thanks Bruce. I'll upgrade to NT7 soon I promise :)

trent

  • Full Member
  • ***
  • Posts: 204
    • View Profile
    • Email
Re: GetValue of Multiple Checkbox Array
« Reply #5 on: November 26, 2013, 02:47:59 PM »
Hi Bruce,

Just tested this in NT7.30 and this is working perfectly. Thank you very much!

Regards,
Trent