|
-
Jun 27th, 2009, 02:05 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] save listbox items into mysql table?
Hi! I want to save all items in a listbox into mysql table. listbox is send through POST. The problem is that I can't access the values of listbox by this way $_POST['listboxname'][0] ...
This way worked $_POST['optionname'] but the problem is I don't know the optionnames in that listbox. they are changing since user can enter new values into the listbox as well as deleting values, and then user press "Save" to send listbox by POST method to php page.
Thank's in advance
-
Jun 27th, 2009, 07:03 AM
#2
Thread Starter
Hyperactive Member
Re: save listbox items into mysql table?
I used this, but a problem still exist, it display only the selected items while I want it to print all items inside the listbox, whether these items selected or not:
Code:
<?php
for ($i=0; $i < count($_POST['mylist']);$i++)
{
echo "hi".$i;
echo $_POST['mylist'][$i];
echo "<br>";
}
?>
<html>
<head></head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<select name="mylist[]" size="5" multiple>
<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>
-
Jun 27th, 2009, 12:10 PM
#3
Re: save listbox items into mysql table?
Well... that's how it's supposed to work, frankly. It's a form element to give the user a number of options and record only the option(s) the user selects.
I don't really understand what you're trying to achieve - can you describe it a bit more?...
-
Jun 28th, 2009, 07:18 AM
#4
Thread Starter
Hyperactive Member
Re: save listbox items into mysql table?
The listbox initially empty. When page loads, the listbox is fill with items(each item is a string - VARCHAR). these items obtained from a mysql table.
Now, the user allowed to add new items or delete items from that listbox(through buttons Add,Remove => they call javascript function to accomplish the task of adding and deleting). After user finished with his/her modification on the items of the list, he click on a button called "Save Changed" this button send the form via POST method to a php page that suppose to save all changes on the mysql table, by removing all items in that table and then add the items in the listbox into that table.
thank's in advance
-
Jun 28th, 2009, 09:47 AM
#5
Re: save listbox items into mysql table?
I see. So it doesn't really matter to you which one the user "selects" (if any) - you just want to get all values that are in the listbox when they submit the form?
In that case, perhaps you could use Javascript to set all <option> elements to "selected" when the user submits the form. I haven't tested this, but it might go something like...
Code:
document.getElementById("myForm").onSubmit = function(){
for(i=0;i<this.dropList.length;i++){
this.dropList[i].selected = true;
}
}
Where "myForm" is the id attribute on your form element, and "dropList" is the name attribute on your select element.
Edit: Alright, I tested it, and this is what worked for me:
Code:
document.getElementById("myForm").onsubmit = function(){
var myList = document.getElementById("myList");
for(i=0;i<myList.length;i++){
this.myList[i].selected = true;
}
}
Had to add the id of "myList" to your drop down, because it was unhappy to refer to a name with brackets in it.
Last edited by SambaNeko; Jun 28th, 2009 at 09:55 AM.
-
Jun 28th, 2009, 01:49 PM
#6
Thread Starter
Hyperactive Member
Re: save listbox items into mysql table?
Thank's alot bro... I tried your code but it didn't work. Here is a test:
Code:
<script>
function tttt(){
var myList = document.getElementById("lstname");
for(i=0;i<myList.length;i++){
this.myList[i].selected = true;
}
}
</script>
<?php
if(isset($_POST['sbbutton'])){
foreach ($_POST['lstname'] as &$value) {
echo $value."<BR>";
}
}
?>
<form name='myform' method='POST' onsubmit='tttt()' action='<?php $_SERVER[PHP_SELF] ?>'>
<select name='lstname[]' multiple size='8'>
<option name='t1'>test1</option>
<option name='t2'>test2</option>
</select>
<input type='submit' name='sbbutton' value='Submit'>
</form>
-
Jun 28th, 2009, 01:52 PM
#7
Thread Starter
Hyperactive Member
Re: save listbox items into mysql table?
I tried this also, but the same! didn't work
Code:
function tttt(){
var i;
for(i=lstname.options.length-1;i>=0;i--)
{
lstname.options[i].selected = true;
}
}
-
Jun 28th, 2009, 01:55 PM
#8
Thread Starter
Hyperactive Member
Re: save listbox items into mysql table?
if anybody have another idea that accomplish the same task please share it with us. Because my idea not practical... deleting all items in the sql table then add all items in the listbox into the sql table... i.e. if user have a big list it will be very inefficient to do it in this way(each time clicking on Save Changes button). until if he add just a new item without deleting or editing another items, script will delete all data in table and add all of them again from the list box.
Thank's in advance
-
Jun 28th, 2009, 08:19 PM
#9
Re: save listbox items into mysql table?
 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>
-
Jun 29th, 2009, 06:03 PM
#10
Thread Starter
Hyperactive Member
Re: save listbox items into mysql table?
It worked! Thank's alot bro
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|