Ok, according to everything I've read so far, this should be working, but it's not. If someone could point out what I did wrong I'd appreciate it. Basically, I have two SELECT form fields that I am passing values between with Javascripts. I am doing a select all action before submittal, but still only getting the last selected value. I should note that I did try adding '[]' to the field name and that broke the Javascript. I'm trying to not have to go back and rewrite that if possible. If I have to, I have to. Included are the array_test form file and array_test2 output/parse and display file. Also, the $myArray will eventually be a query to a db. I am just using a test array as I debug this.
Input file:
<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Array Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<?php
$myArray = array(0,1,2,3,4,5,6,7,8,9);
$myArray2 = array();
print "<script language='JavaScript1.2'>";
print "var myJSArray = new Array(); var myJSArray2 = new Array(); myJSArray = [";
foreach ($myArray as $jsArray) {
print "'" . $jsArray . "',";
}
print "];";
print "</script>";
?>
<script language='Javascript'>
function move(fbox, tbox) {
var arrFbox = new Array();
var arrTbox = new Array();
var arrLookup = new Array();
var i;
for (i = 0; i < tbox.options.length; i++) {
arrLookup[tbox.options[i].text] = tbox.options[i].value;
arrTbox[i] = tbox.options[i].text;
}
var fLength = 0;
var tLength = arrTbox.length;
for(i = 0; i < fbox.options.length; i++) {
arrLookup[fbox.options[i].text] = fbox.options[i].value;
if (fbox.options[i].selected && fbox.options[i].value != "") {
arrTbox[tLength] = fbox.options[i].text;
tLength++;
}
else {
arrFbox[fLength] = fbox.options[i].text;
fLength++;
}
}
arrFbox.sort();
arrTbox.sort();
fbox.length = 0;
tbox.length = 0;
var c;
for(c = 0; c < arrFbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrFbox[c]];
no.text = arrFbox[c];
fbox[c] = no;
}
for(c = 0; c < arrTbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrTbox[c]];
no.text = arrTbox[c];
tbox[c] = no;
}
}
function submitQList() {
for (i=0; i < document.arrayTest.qList2.length; i++) {
document.arrayTest.qList2.selected = true;
}
document.arrayTest.submit();
}
</script>
</head>
<body>
This page is set to test the creation and manipulation of arrays generated in PHP with Javascript.
<br /><br />
<form action="array_test2.php" method="post" name="arrayTest">
<table width='100%'>
<tr>
<td valign='top'>
<select name="qList1" size="20" multiple="multiple">
<script language='JavaScript1.2'>
function genArray() {
for (i = 0; i < myJSArray.length; i++) {
document.write ("<option value='" + myJSArray + "'>" + myJSArray + "</option>");
}
}
genArray();
</script>
</select>
</td>
<td valign='middle'>
<input type="button" onClick="move(this.form.qList2,this.form.qList1);" value="<<">
<input type="button" onClick="move(this.form.qList1,this.form.qList2);" value=">>">
</td>
<td valign='top'>
<select name="qList2" size="20" multiple="multiple">
<script language='JavaScript1.2'>
function genArray2() {
for (i = 0; i < myJSArray2.length; i++) {
document.write ("<option value='" + myJSArray2 + "'>" + myJSArray2 + "</option>");
}
}
genArray2();
</script>
</select>
</td>
</tr>
</table>
<br />
<!-- <input type="submit" onClick="submitQList();" name="Submit" value="Submit" /> -->
<input type='checkbox' name='test[0]' value='1' />One<br />
<input type='checkbox' name='test[1]' value='2' />Two<br />
<input type='checkbox' name='test[2]' value='3' />Three<br />
<input type='checkbox' name='test[3]' value='4' />Four<br />
<br />
<input type="button" onClick="submitQList();" name="Submit" value="Submit" />
</form>
</body>
</html>
Form action parse/display file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
print count($HTTP_POST_VARS['test']) . "<br />";
print count($HTTP_POST_VARS['qList1']) . "<br />";
print count($HTTP_POST_VARS['qList2']) . "<br />";
$form_fields = array_keys($HTTP_POST_VARS);
for ($i = 0; $i < count($form_fields); $i++) {
$thisField = $form_fields[$i];
$thisValue = $HTTP_POST_VARS[$thisField];
echo $thisField ." = ". $thisValue;
echo "<br />";
}
?>
</body>
</html>