PDA

View Full Version : Is there a way to make PHP do the job of Javascript?



BLiZZaRD
11-12-2005, 01:13 PM
Hello again! I have a really nice code here, basically it is 2 multi select boxes with which you can select one object and place it in the other box.

As you can see by the code here, there is a TON of <javascript>. Needless to say this makes for one messy source code.

Basically I want the whole thing to be php based:



<?
function MultiAvailSelectBox($availableTitle,$selectedTitle,$availableList,$selectedList,$listAvailVar='listavail',$listSelVar='listsel',$size=5) {
?>
<SCRIPT LANGUAGE="JavaScript">
<!--
function SelectAll(selectItem) {
var i;
var j = 0;
for (i = 0; i < selectItem.options.length; i++) {
selectItem.options[j].selected = true;
j++;
}
}

function addItems(fromItem, toCtrl) {
var i;
var j;
var itemexists;
var nextitem;

// step through all items in fromItem
for (i = 0; i < fromItem.options.length; i++) {
if (fromItem.options[i].selected) {
// search toCtrl to see if duplicate
j = 0;
itemexists = false;
while ((j < toCtrl.options.length) && (!(itemexists))) {
if (toCtrl.options[j].value == fromItem.options[i].value) {
itemexists = true;
alert(fromItem.options[i].value + " found!... Possibly a duplicate key in the arrays");
}
j++;
}
if (!(itemexists)) {
// add the item
nextitem = toCtrl.options.length;
toCtrl.options[nextitem] = new Option(fromItem.options[i].text);
toCtrl.options[nextitem].value = fromItem.options[i].value;
}
}
}
}


function removeItems(fromItem) {
var i = 0;
var j;
var k = 0;

while (i < (fromItem.options.length - k)) {
if (fromItem.options[i].selected) {
// remove the item
for (j = i; j < (fromItem.options.length - 1); j++) {
fromItem.options[j].text = fromItem.options[j+1].text;
fromItem.options[j].value = fromItem.options[j+1].value;
fromItem.options[j].selected = fromItem.options[j+1].selected;
}
k++;
} else {
i++;
}
}
for (i = 0; i < k; i++) {
fromItem.options[fromItem.options.length - 1] = null;
}
}
//-->
</SCRIPT>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th valign="top" width="45%"><?=$availableTitle?></th>
<th valign="top" width="10%">&nbsp</th>
<th valign="top" width="45%"><?=$selectedTitle?></th>
</tr>
<tr>
<td align="center" valign="top">
<select multiple name="<?=$listAvailVar?>[]" size="<?=$size?>">
<?
while (list($key,$val)=each($availableList)) {
?>
<option value="<?=$key?>"><?=$val?></option>
<?
}
?>
</select>
</td>
<td align="center" valign="center">
<input type="button" name="AddBtn" value=" >> " OnClick="addItems(this.form.elements['<?=$listAvailVar?>[]'], this.form.elements['<?=$listSelVar?>[]']); removeItems(this.form.elements['<?=$listAvailVar?>[]']);"><br>
<input type="button" name="RemoveBtn" value=" << " OnClick="addItems(this.form.elements['<?=$listSelVar?>[]'], this.form.elements['<?=$listAvailVar?>[]']); removeItems(this.form.elements['<?=$listSelVar?>[]']);">
</td>
<td align="center" valign="top">
<select multiple name="<?=$listSelVar?>[]" size="<?=$size?>">
<?
while (list($key,$val)=each($selectedList)) {
?>
<option value="<?=$key?>"><?=$val?></option>
<?
}
?>
</select>
</td>
</tr>
<tr>
<td colspan="3" align="center" valign="center">
<input type="button" value="Reset" OnClick="SelectAll(this.form.elements['<?=$listAvailVar?>[]']); SelectAll(this.form.elements['<?=$listSelVar?>[]']); this.form.submit();">
</td>
</tr>
</table>
<?
}
?>

<?
// EXAMPLE USAGE
if (!isset($show)) {
?>
<form name="main" action="<?=$PHP_SELF?>" method="get">
<?
$availableList=Array('1'=>'Tiger','3'=>'Asp','4'=>'Kodiak', '8'=>'Cheetah');
$selectedList=Array('2'=>'Lion','5'=>'Frog', '6'=>'Baboon','7'=>'Elephant');
MultiAvailSelectBox("On Ark","Off Ark",$availableList,$selectedList,'available','selected',10)
?>
<input type="hidden" name="show" value="yes">
</form>
<?
} else {
?>
<hr>
$available = <br>
<pre>
<?print_r($available);?>
</pre>
<hr>
$selected = <br>
<pre>
<?print_r($selected);?>
</pre>
<?
}
?>


I hope there is because I have other plans for things inside the source code, and with the code the way it is they would get lost very quickly!

Thanks for any/all help!