views:

52

answers:

2

Hi,

I have been working on this for 1 hour with no such luck. Any help would be appreciated.

I have a cgi script with which creates these select param values:

print "Please select a dkversion to compare : <br><br>";
print "<select name='dkversion' id='dkversion' size='6' multiple='multiple'>";
foreach my $values ('ASDF123GS v0.01 models eval QA <-> apple', 'ZXCV534GS v1.01 models eval QA <-> pineapple')
{
    print "<option value=\"" . $values . "\" >" . $values  . "</option>";
}
print "</select>";
print "</form>";

I have another html page that uses jquery/javascript to process the inputs:

     var scalarstr = "";
     $("#dkversion :selected").each(function () {
        scalarstr += "dkselected=" + encodeURIComponent($(this).val()) + "&";
     });

     $.get("./scripts/modelQA_correlation.cgi?" +  scalarstr + "&menu_mode=2",function(data){ 
     });

Coming Back to cgi page to process the multiple selects, I do a dump of the inputs and noticed it isn't separating the values:

$VAR1 = { 'dkselected' => 'ASDF123GS v0.01 models eval QA <-> apple�ZXCV534GS v1.01 models eval QA <-> pineapple', 'menu_mode' => '2' }; 

Why isn't the dkselected values being separate into it's two parts??

+1  A: 

A safer approach here is to let jQuery encode the string, let's get the values from the <select>, store them in an array, via .serializeArray(), then add the menu_mode to it, like this:

var params = $("#dkversion").serializeArray()​;
params.push({ name: 'menu_mode', value: '2' });
$.get("./scripts/modelQA_correlation.cgi", params, function(data){ });​

You can test it out here. This does everything your code above does, but I hope you'll agree much simpler and easier to maintain. This works by passing an object as the $.get() data option, which internally calls $.param() to get the final string, so you can test/see the result yourself like I have in the demo.

Nick Craver
ooo thanks, i will give this a try
Gordon
A: 

If you are doing something like $args = CGI::Vars, then you're running afoul of the quirk in how CGI.pm handles multiple values. You'll need to split the string on "\0" (null).

Another approach is to use the param method: @vals = $q->param('dkversion');

RickF
everything about the coding was correct except i didn't save into @vals, i was thinking the data would have been processed in the hash but it wasn't ...
Gordon