I am attempting to setup a multiple select box page that has 1 master select box and a dynamic number of child ones.
For example: I have 10 territories and within those 10 territories can be 100 counties. I am using jquery to with buttons "Add" and "Remove" to move these counties between the different selector boxes.
Now the challange is to create javascript to submit the changes via ajax. I know I am doing this the hard way right now by making the javascript dynamic with PHP so I am wondering a more simple way, as well as how I can pass those variables into the data string.
Right now I am using this:
<script type="text/javascript">
$().ready(function() {
<?
$territories = $db->get_results("SELECT * FROM tbl_territories");
foreach ($territories as $territory) {
echo "
$('#remove-{$territory->id}').click(function() {
return !$('#territory-{$territory->id} option:selected').remove().appendTo('#county');
$.ajax({
type: 'POST',
url: 'add.territoryselect.php',
data: {
action: 'remove',
county: $(button).siblings('[name=/"ClientId/"]').attr(/"id/"),
success: function(msg){
alert( 'Data Saved: ' + msg );
}
});
});
$('#add-{$territory->id}').click(function() {
return !$('#county option:selected').remove().appendTo('#territory-{$territory->id}');
});";
}
?>
});
</script>
The page looks like this:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<?
foreach ($territories as $territory) {
echo "
<tr valign='middle'>
<td align='center'>
<a href='#'>{$territory->name}</a><br>
</td>
<td align='center'><label for='county'></label>
<select style='width: 200px;' name='territory-{$territory->id}' size='5' multiple='multiple' id='territory-{$territory->id}'>";
foreach ($counties as $county) {
if ($county->territory == $territory->id) {
echo "<option value='{$county->id}'>{$county->county}, {$county->state}</option>";
}
}
echo "</select>
</td>
<td align='center'><input type='submit' class='button green size-120 fl' value='<< Add' id='add-{$territory->id}'/><br /><br />
<input type='submit' class='button fl size-120' value='Remove >>' id='remove-{$territory->id}' />
</td>
</tr>";
}
?>
</table></td>
<td align="center" valign="top"><select style="width: 200px;" name='county' size='50' multiple='multiple' id='county'>
<?
foreach ($counties as $county) {
if (is_null($county->territory)) {
echo "<option value='{$county->id}'>{$county->county}, {$county->state}</option>";
}
}
?>
</select></td>
</tr>
</table>
Obviously this page is not working right now as the ajax section is incomkplete as I am sure there is a better way to do this but my jquery/javascript knowledge is lacking.
Thanks for any help!