save ALL the items of a HTML listbox to mysql db

Hi, i’m making a webform to manage some data. The data needs to be saved in a mysql database. On that form there is a listbox where the user can add items to. This i don’t have any problems with. There is also a save button on the form, wich needs to save all the data (so also all the listbox items). I have a few problems with this. 1) I use the $_POST[Nameoflistbox] function to get the items of my listbox, but this only gives me the selected items 2) Then i use a for loop to run through my items, and to get the option value i use $tmpNameArray[$i]. But this only gives me the first character of my optionValue?
Can anybody help me out please, i’m pretty new to PHP/Javascript. Here is a simplified form wich shows my problem:

<?php include "../connectDb.php"; ?> 500 gram 600 gram 700 gram <?php if ($_SERVER['REQUEST_METHOD'] == "POST") { if (isset($_POST['Save'])) { $Units=$_POST[units]; echo count($Units); for($i=0;$i<count($Units);$i++) { mysql_query("INSERT INTO Article (ItemId, Unit) VALUES (10,$Units[$i])"); }
    }
}

?>

If i don’t select a value in my listbox, the echo count($Units); line shows 0, if i select a value then it shows 1.

If i select the second item, then 6 is inserted in the Unit column of my db.

Thanx in advance for your help.

Standard element return scalar value, not an array. So in your PHP code, $_POST[‘units’] will be single value, not an array. Therefore it not make sense to use array index like $_POST[‘units’][1], as this will point to a certain character in a string value $_POST[‘units’].
You can use if you need to receive array of selected items. But, again, this will return only list of selected items (as usually, user will need to hold Ctrl while selecting multiple items in list).

If you need to get in php code ALL the items from your select list, you can just create hidden fields in addition to your select element in your form, like this:

[php]

500 gram 600 gram 700 gram [/php]

Ok i will give it a shot. As i give the user the posibility to dynamicly add items to the listbox, i will probably also have to have a hidden field to store the number of items?

Thanx for your reply

Sponsor our Newsletter | Privacy Policy | Terms of Service