Jump to content

multiple select list issue with PHP and javascript


jwhite68

Recommended Posts

I have an issue with multiple select lists, javascript and PHP.

 

I have two multiple select lists, list1 and list2. The idea is that items in list1 can be moved individually from list1 to list2.

 

At the end, I want to pass the values from list2, as an array, back to be processed within PHP.

The problem currently is that with name="list2", I dont think PHP is seeing it as an array, and I need it to be name="list2[]", but then when I change it to this, I get a javascript error at runtime - so javascript doesnt like the [].

 

Can anyone advise a way around this? See PHP code section below - which you can see generates the javascript via echo commands.

 

<? if ($type=="single")

{

echo '

<div class="d841"><strong>  05. Country to display product in</strong>'.$errs[1].'<a name="err1"></a></div>

<div class="t841" id="title">

<table border="0" cellspacing="0" class="formcontent">

<tr>

<td class="td135" style="text-align:right !important"><span'.$hlight1.'>Country</span></td>

<td colspan="5" class="td675">

<select name="country" style="width:385px">

<option value="">Select country</option>';

 

foreach ($countries as $key => $value)

{ echo '<option value="'.$key.'">'.$value.'</option>'; }

 

echo '</select>

</td>

</tr>

</table>

</div>';

}

else

{

echo '

<div class="d841"><strong>  05. Countries to display product in</strong></div>

<div class="t841" id="title">

<table border="0" cellspacing="0" class="formcontent">

<tr>

<td class="td135" style="text-align:right !important"><span>Country</span></td>

<td colspan="5" class="td675">

<select multiple size="10" name="list1" style="width:150">';

foreach ($countries as $key => $value) { echo '<option value="'.$key.'">'.$value.'</option>'; }

echo '</select>

<input type="button" onClick="move(this.form.list2,this.form.list1)" value="<<" style="width:30px" />

<input type="button" onClick="move(this.form.list1,this.form.list2)" value=">>" style="width:30px" />

<select multiple size="10" name="list2" style="width:150"></select>

</td>

</tr>

</table>

</div>';

}

?>

 

Keep the names as "list1[]" (for PHP posting) but address them via their id in the js script eg

 

<html>
<head>
<meta name="generator" content="PhpED Version 4.5 (Build 4513)">
<title>sample</title>
<meta name="author" content="Barand">
<link rel="shortcut icon"  href="">

<script language='javascript'>
          function click1(id) {
            var $obj = document.getElementById(id);
            alert ($obj.name + " : Value " + $obj.value);
          }
</script>

</head>
<body>
<form name='fm1' method='get'>
    <select name='list1[]' id='list1' size='3' onclick='click1(this.id)'>
      <option value='A'>A</option>
      <option value='B'>B</option>
    </select>
    <select name='list2[]' id='list2' size='3' onclick='click1(this.id)'>
      <option value='C'>C</option>
      <option value='D'>D</option>
    </select>
</form>

</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.