Greets,
I searched for an answer before I posted - my apologies if this has been asked in the past.
I have a PHP class that creates a MultiListPickBox (two select boxes, side by side, with some special JS functionality to move items from the left box to the right and vice versa upon the pressing of a button).
The script works great and the values transfer between the two boxes without issue. However, only Javascript is changing the code - my object properties are not changing to reflect the user's choices. Is there a way I can update my object properties in PHP on a Javascript event?
The source is a little long, but I'll include it for berevity. If you want to try it out, make sure you declare the object within <form> tags. It's in development, so please bear with me :-)
$MultiPickListBox = new CMultiPickListBox();
$Item1 = array_merge("1","Test1");
$Item2 = array_merge("2","Test2");
$MultiPickListBox->AddSourceItem($Item1);
$MultiPickListBox->AddSourceItem($Item2);
<!-- BODY CONTENT GOES HERE -->
<form action="Test.php" method=post name=form>
<?php
$MultiPickListBox->Create(0,
"<font face=\"Verdana, Arial, Helvetica, Sans-Serif\" size=-2>Available Items</font>",
"<font face=\"Verdana, Arial, Helvetica, Sans-Serif\" size=-2>Selected Items</font>");
?>
<input type=submit name=Submit value="Process Items">
<input type=hidden name=MultiPickListBox value=<? echo urlencode(serialize($MultiPickListBox)); ?>><br>
</form>
<!-- BODY CONTENT ENDS HERE -->
<?php
class CMultiPickListBox {
var $m_SrcResults;
var $m_DstResults;
function CMultiPickListBox(){
//Constructor
$ProcName = "CMultiPickListBox::CMultiPickListBox";
// Initialize arrays
$this->m_SrcResults = array();
$this->m_DstResults = array();
}
function AddSourceItem($SourceItem) {
$ProcName = "CMultiPickListBox::AddSourceItem";
array_push($this->m_SrcResults, $SourceItem);
}
function GetSourceItem($Index) {
$ProcName = "CMultiPickListBox::GetSourceItem";
return $this->m_SrcResults[$Index];
}
function GetSourceItemCount() {
$ProcName = "CMultiPickListBox::GetSourceItemCount";
return count($this->m_SrcResults);
}
function AddDestinationItem($DestinationItem) {
$ProcName = "CMultiPickListBox::AddDestinationItem";
array_push($this->m_DstResults, $DestinationItem);
}
function GetDestinationItem($Index) {
$ProcName = "CMultiPickListBox::GetDestinationItem";
return $this->m_DstResults[$Index];
}
function GetDestinationItemCount() {
$ProcName = "CMultiPickListBox::GetDestinationItemCount";
return count($this->m_DstResults);
}
function Create($count, $left = "", $right=""){
$ProcName = "CMultiPickListBox::Create";
?>
<SCRIPT LANGUAGE="JavaScript">
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
sortitems = 1; // Automatically sort items within lists? (1 or 0)
function move(fbox,tbox) {
for(var i=0; i<fbox.options.length; i++) {
if(fbox.options[i].selected && fbox.options[i].value != "") {
var no = new Option();
no.value = fbox.options[i].value;
no.text = fbox.options[i].text;
tbox.options[tbox.options.length] = no;
fbox.options[i].value = "";
fbox.options[i].text = "";
}
}
BumpUp(fbox);
if (sortitems) SortD(tbox);
}
function moveall(fbox,tbox){
for (var i=0; i<fbox.options.length; i++) {
if (fbox.options[i].value != "") {
var no = new Option();
no.value = fbox.options[i].value;
no.text = fbox.options[i].text;
tbox.options[tbox.options.length] = no;
fbox.options[i].value = "";
fbox.options[i].text = "";
}
}
BumpUp(fbox);
if (sortitems) SortD(tbox);
}
function BumpUp(box) {
for(var i=0; i<box.options.length; i++) {
if(box.options[i].value == "") {
for(var j=i; j<box.options.length-1; j++) {
box.options[j].value = box.options[j+1].value;
box.options[j].text = box.options[j+1].text;
}
var ln = i;
break;
}
}
if(ln < box.options.length) {
box.options.length -= 1;
BumpUp(box);
}
}
function SortD(box) {
var temp_opts = new Array();
var temp = new Object();
for(var i=0; i<box.options.length; i++) {
temp_opts[i] = box.options[i];
}
for(var x=0; x<temp_opts.length-1; x++) {
for(var y=(x+1); y<temp_opts.length; y++) {
if(temp_opts[x].text > temp_opts[y].text) {
temp = temp_opts[x].text;
temp_opts[x].text = temp_opts[y].text;
temp_opts[y].text = temp;
temp = temp_opts[x].value;
temp_opts[x].value = temp_opts[y].value;
temp_opts[y].value = temp;
}
}
}
for(var i=0; i<box.options.length; i++) {
box.options[i].value = temp_opts[i].value;
box.options[i].text = temp_opts[i].text;
}
}
// End -->
</script>
<center>
<table border="0" width=100%>
<?
if (isset($left)){
echo "<tr><td width=45%>$left</td>";
if (isset($right)){
echo "<td width=10%></td><td width=45%>$right</td>";
}
echo "</tr>";
}
?>
<tr>
<td width=45%><select multiple size="5" STYLE="width=100%" name="list<?php echo $count;?>">
<?php
for ($i = 0; $i < $this->GetSourceItemCount(); $i++){
// Dump the key-value pairs
list($key, $val) = $this->GetSourceItem($i);
echo "<option value='" . $key . "'>" .
$val . "</option>";
}
?>
</select>
</td>
<td width=10% valign=middle align=center>
<input type="button" value=">>"
onclick="moveall(this.form.list<?php echo $count;?>,this.form.list2<?php echo $count;?>)"
name="A1"><br>
<input type="button" value="> "
onclick="move(this.form.list<?php echo $count;?>,this.form.list2<?php echo $count;?>)"
name="B1"><br>
<input type="button" value="< "
onclick="move(this.form.list2<?php echo $count;?>,this.form.list<?php echo $count;?>)"
name="B2"><br>
<input type="button" value="<<"
onclick="moveall(this.form.list2<?php echo $count;?>,this.form.list<?php echo $count;?>)"
name="A2">
</td>
<td width=45%><select multiple size="5" STYLE="width=100%" name="list2<? echo $count;?>">
<?php
for ($i = 0; $i < count($this->m_DstResults); $i++){
// Dump the key-value pairs
list($key, $val) = $this->GetDestinationItem($i);
echo "<option value='" . $key . "'>" .
$val . "</option>";
}
?>
</select>
</td>
</tr>
</table>
</center>
<input type=hidden name="DestOptions<?php echo $count;?>">
<?
}
}
?>