View Full Version : PHP & JS problem

01-23-2004, 07:32 PM
Hi, I'm having some problems with my PHP and javascript codes.
I'm doing a form with two multiple lists to select users, so I have two buttons that help me to add users to the other list and delete them from it. Here is the code:

<form name="acta" action="<?=$_SERVER['REQUEST_URI']?>" method="POST">
<select name="example" style="width:195px" size="1" onChange="redirect(this.options.selectedIndex)">
//This is the dropdown that fills the 1st list, there is no problem with this
$query_dep="SELECT * FROM dep";
echo "<option value=\"".$dep['id_dep']."\">".$dep['nom_dep']."</option>";
<select name="stage2" style="width:215px" size="5" multiple="multiple">
//This is the 1st list. I have to fill it with some values when the page loads for the first time
$query_stage2="SELECT * FROM user u,dep d WHERE u.dep_us=d.id_dep AND d.nom_dep='whatever'";
echo "<option value=\"".$stage2['id_us']."\">".$stage2['nom_us']."</option>";
<!--These are the buttons-->
<input type="button" class="azul2" name="test" value=">>" onClick="Add()" />
<br /><br />
<input type="button" class="azul2" name="test2" value="<<" onClick="Del()" />
<!--3rd list-->
<select name="stage3" style="width:215px" size="5" multiple="multiple">

and here are the javascript functions

function Add() {
var srcList = document.acta.stage2;
var tgtList = document.acta.stage3;

var srcLen = document.acta.stage2.length;
var tgtLen = document.acta.stage3.length;
var tgt = "x";

//build array of target items
for (var i=tgtLen-1; i > -1; i--) {
tgt += "," + tgtList.options[i].value + ","

//Pull selected resources and add them to list
for (var i=srcLen-1; i > -1; i--) {
if (srcList.options[i].selected && tgt.indexOf( "," + srcList.options[i].value + "," ) == -1) {
opt = new Option( srcList.options[i].text, srcList.options[i].value );
tgtList.options[tgtList.length] = opt;

function Del() {
var srcList = document.acta.stage3;
var srcLen = document.acta.stage3.length;

for (var i=srcLen-1; i > -1; i--) {
if (srcList.options[i].selected) {
srcList.options[i] = null;

So I fill the 3rd list with the Add() function, and everything's fine, but when the form is submitted, PHP don't recognize any element of the list, and nothing is passed through $_POST['stage3'].
Someone told me n another thread (don't remember who) that you can pass multiple values by putting the element like this:

<select name="stage3[]" style="width:215px" size="5" multiple="multiple">

And that's right. It passes an array with the submitted values (I recover them with a foreach). But the problem here, is that javascript sends the 'document.acta.stage3.length is not an object or it is null' error...

How can I do this without PHP or Javascript errors??