Click to See Complete Forum and Search --> : Passing multiple select values


esiason14
05-13-2007, 12:23 AM
I have several multiple select boxes on a page

Example

<div id='tabs0' style='position:absolute; top:0; left:0; visibility: visible; margin: 10px;'>
<select multiple id=1 name=1 style='font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif; font-size: 11px; width: 350; height: 180' onChange='add_player(this, "1"); return true;' size=10>
<?php
$count = count($cbs_id);
for($i=0;$i<$count;$i++)
{
if ($pos[$i] == "1")
{
{
echo "<option value=".$cbs_id[$i].">".$lname[$i].", ".$fname[$i]."\n";
}
}
}
?>
</select></div>

I'm trying to figure out how I can pass multiple values if the user selects more than one.

var c = document.getElementById('1').value;
var fb = document.getElementById('2').value;
var queryString = "?submit=true&1=" + c + "&2=" + fb;

ajaxRequest.open("GET", "newmlbcompare2.php" + queryString, true);
ajaxRequest.send(null);

If there are multiple c and fb values...how can I pass all of those? Any help would be appreciated!!

Weedpacket
05-13-2007, 03:17 AM
Don't have an answer off the top of my head (and I've gotta be gone in a couple of seconds), but I do notice that your ID attributes have invalid values (as well as being unquoted).

esiason14
05-13-2007, 03:22 AM
Thanks...I've changed the id's.

Ok, that's cool. If you get a chance some other time...I could still use the help!

Weedpacket
05-13-2007, 08:21 AM
Well, the name of the multiple-entry field that is submitted would have a name that ends with "[]"; PHP will parse that as an array of values. (reference: http://www.php.net/manual/en/faq.html.php#faq.html.select-multiple ).

In Javascript, the properties of an HTML select Multiple object are a little tedious to get to: for consistency with other fields, selectElement.value only returns one selected value. You'll need to loop through the element's options array and look at the selected property of each one.

// Just making an array of selected elements, here.
var selectElement = document.getElementById('multiple_select_element');
var selectedValues = [];
var len = selectElement.length;
for(var i=0; i<len; i++)
{
var option = selectElement.options[i];
if(option.selected)
selectedValues.push(opt.value);
return
}

esiason14
05-13-2007, 11:57 AM
Ok. I think I get that. I'm very new to javascript/AJAX so I'm trying to walk through it.

To give you an idea of what I'm doing...here's a link (http://www.fantasyplaymakers.com/newmlbcompare.php)

Notice if you select one player per tab and hit submit...it grabs that value. I'm still trying to figure out how to use your snippet to pass all the values if multiples of one position are selected. If you could give me some more tips..that would be great

esiason14
05-13-2007, 02:47 PM
I'm not sure this is optimal, but I just read about Prototype's "Form.serialize (http://www.prototypejs.org/api/form/serialize)"

So doing this:

var queryString = Form.serialize(myformname);
ajaxRequest.open("POST", "newmlbcompare2.php?" + queryString);

passing the parameters like this:

["C"]=> array(1) { [0]=> string(18) "174886,18657,18678" }

Check it out here (http://www.fantasyplaymakers.com/newmlbcompare.php)