
Originally Posted by
Visual Basic.Net
Thank's alot bro... I tried your code but it didn't work.
Well you kinda changed it a bit... This is what I had tested, based on your original code:
Code:
<?php
for ($i=0; $i < count($_POST['mylist']);$i++)
{
echo $_POST['mylist'][$i];
echo "<br>";
}
?>
<html>
<head></head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>" id="myForm">
<select name="mylist[]" size="5" multiple id="myList">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
</select>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
<script>
document.getElementById("myForm").onsubmit = function(){
var myList = document.getElementById("myList");
for(i=0;i<myList.length;i++){
this.myList[i].selected = true;
}
}
</script>
The way you changed it won't work for a couple of reasons.
1. var myList = document.getElementById("lstname");
This won't work because it's looking for an element with id='lstname'. You don't have one.
2. this.myList[i].selected = true;
I don't think this will work because the "this" in my context referred to the form element, since the function was bound to its "onsubmit" action. I'm not sure if it's referring to the right thing in yours...
3. onsubmit='tttt()'
This is a problem because it's calling the tttt() function, but it's not waiting for it to finish before submitting the form. So tttt() isn't getting to finish its work.
Fixing it would look like this:
Code:
<script>
function tttt(){
var myList = document.getElementById("lstname");
for(i=0;i<myList.length;i++){
myList[i].selected = true;
}
return true;
}
</script>
<?php
if(isset($_POST['sbbutton'])){
foreach ($_POST['lstname'] as &$value) {
echo $value."<BR>";
}
}
?>
<form name='myform' method='POST' onsubmit='return tttt()' action='<?php $_SERVER[PHP_SELF] ?>'>
<select name='lstname[]' multiple size='8' id="lstname">
<option>test1</option>
<option>test2</option>
</select>
<input type='submit' name='sbbutton' value='Submit'>
</form>