JavaScript and client side scripting.
Moderator: General Moderators
kalpesh
Forum Commoner
Posts: 54 Joined: Sun Sep 21, 2008 5:04 am
Post
by kalpesh » Sun May 17, 2009 12:58 am
Hi,
I try the to fill one box from another list box,then insert the value in second list box in database.
I successfully enter the value in another box from one box but now it is not inserting values from thart second box.
Here is my code :
Code: Select all
<script language=javascript>
var from_array = new Array('Pink',1,2,'Green',4,'Yellow'); // this array has the values for the source list
var to_array = new Array('Red',3,5); // this array has the values for the destination list(if any)
</script>
<script language=javascript>
function moveoutid()
{
var sda = document.getElementById('xxx');;
var len = sda.length;
var sda1 = document.getElementById('yyy');
for(var j=0; j<len; j++)
{
if(sda[j].selected)
{
var tmp = sda.options[j].text;
var tmp1 = sda.options[j].value;
sda.remove(j);
j--;
var y=document.createElement('option');
y.text=tmp;
try
{sda1.add(y,null);
}
catch(ex)
{
sda1.add(y);
}
}
}
}
function moveinid()
{
var sda = document.getElementById('xxx');
var sda1 = document.getElementById('yyy');
var len = sda1.length;
for(var j=0; j<len; j++)
{
if(sda1[j].selected)
{
var tmp = sda1.options[j].text;
var tmp1 = sda1.options[j].value;
sda1.remove(j);
j--;
var y=document.createElement('option');
y.text=tmp;
try
{
sda.add(y,null);}
catch(ex){
sda.add(y);
}
}
}
}
</script>
<form action="post.php" name="f3">
<table border=1 align=center valign=center>
<tr><td>From Group</td><td></td><td>To Group</td></tr>
<tr><td>
<select id=xxx multiple size=7 style="width:100;">
<script language=javascript>
for(var i=0;i<from_array.length;i++)
{
document.write('<option>'+from_array[i]+'</option>');
}
</script>
</select>
</td>
<td>
<input type=button value=">>" onclick=moveoutid()>
<input type=button value="<<" onclick=moveinid()>
</td>
<td>
<select id=yyy multiple size=7 style="width:100;">
<script language=javascript>
for(var j=0;j<to_array.length;j++)
{
document.write('<option>'+to_array[j]+'</option>');
}
</script>
</select>
</td></tr>
<input type="button" name="submit" value="Submit" >
</form>
</table>
Here is my post.php
Code: Select all
$yyy = $_POST['yyy'];
echo $_POST['yyy'];
When i try to echo the post value then it will not displaying the post value from second list box.
Please help me thanks in advance.
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Sun May 17, 2009 2:11 am
There are no javascript onchange events assigned to your select menus. This is not PHP related. Javascript is client side.
Moved to Javascript
mavieng
Forum Newbie
Posts: 10 Joined: Thu Jan 29, 2009 12:34 pm
Post
by mavieng » Sun May 17, 2009 11:24 am
Mistakes in your code code you didn't specify name attribute for select box. Also default method for form is GET which needs to POST to get POST variable.*/
If you want to recieve multiple values from select box then you should use name of field like name='yyy[]'. If you will use name='yyy' it will return only last selected value out of all.*/
Code: Select all
<script language=javascript>
var from_array = new Array('Pink',1,2,'Green',4,'Yellow'); // this array has the values for the source list
var to_array = new Array('Red',3,5); // this array has the values for the destination list(if any)
</script>
<script language=javascript>
function moveoutid()
{
var sda = document.getElementById('xxx');;
var len = sda.length;
var sda1 = document.getElementById('yyy');
for(var j=0; j<len; j++)
{
if(sda[j].selected)
{
var tmp = sda.options[j].text;
var tmp1 = sda.options[j].value;
sda.remove(j);
j--;
var y=document.createElement('option');
y.text=tmp;
try
{sda1.add(y,null);
}
catch(ex)
{
sda1.add(y);
}
}
}
}
function moveinid()
{
var sda = document.getElementById('xxx');
var sda1 = document.getElementById('yyy');
var len = sda1.length;
for(var j=0; j<len; j++)
{
if(sda1[j].selected)
{
var tmp = sda1.options[j].text;
var tmp1 = sda1.options[j].value;
sda1.remove(j);
j--;
var y=document.createElement('option');
y.text=tmp;
try
{
sda.add(y,null);}
catch(ex){
sda.add(y);
}
}
}
}
</script>
<form action="a.php" name="f3" method='post'>
<table border=1 align=center valign=center>
<tr><td>From Group</td><td></td><td>To Group</td></tr>
<tr><td>
<select id=xxx name='xxx' multiple size=7 style="width:100;">
<script language=javascript>
for(var i=0;i<from_array.length;i++)
{
document.write('<option>'+from_array[i]+'</option>');
}
</script>
</select>
</td>
<td>
<input type=button value=">>" onclick=moveoutid()>
<input type=button value="<<" onclick=moveinid()>
</td>
<td>
<select id=yyy name='yyy[]' multiple size=7 style="width:100;">
<script language=javascript>
for(var j=0;j<to_array.length;j++)
{
document.write('<option>'+to_array[j]+'</option>');
}
</script>
</select>
</td></tr>
<input type="submit" name="submit" value="Submit" >
</form>
</table>
<br><br><br><br><br>
<?php
echo $_POST['yyy'];
foreach($_POST['yyy'] as $key=>$value)
{
echo $key . " = " . $value;
}
?>
Last edited by
Benjamin on Mon May 18, 2009 10:17 am, edited 1 time in total.
Reason: Changed code type from text to javascript. Removed bold bb tags.