Unselected Listbox values
Unselected Listbox values
(OP)
Another newbie question!
I know you can get the option value of selected items in a listbox from the $_POST variable, but is it possible to determine all items in a listbox (selected and unselected) following a post submit.
What I am trying to achieve is:
A form has 2 listboxes. By selecting a name in listbox1 it is transferred to listbox2. When the selection of names is finished clicking on a SAVE button will allow the option values of all the items in listbox2 to be saved to a data table without having to select all the items in listbox2 before clicking on the SAVE button.
Many thanks
I know you can get the option value of selected items in a listbox from the $_POST variable, but is it possible to determine all items in a listbox (selected and unselected) following a post submit.
What I am trying to achieve is:
A form has 2 listboxes. By selecting a name in listbox1 it is transferred to listbox2. When the selection of names is finished clicking on a SAVE button will allow the option values of all the items in listbox2 to be saved to a data table without having to select all the items in listbox2 before clicking on the SAVE button.
Many thanks
RE: Unselected Listbox values
Nope, that unfortunately was not designed to be used like that.
In various projects I worked with 2 workarounds :
- listbox2 has multiple attribute set and onsubmit a script selects all options
- use an additional hidden input and onsubmit a script copies all options' values as comma ( or whatever matches your data ) separated enumeration to the input
Personally I find the 1st one messy so I prefer the 2nd one.Feherke.
feherke.github.io
RE: Unselected Listbox values
RE: Unselected Listbox values
I guess you already have some JavaScript there. If you post it, we can suggest a solution that plays nice with your current code. ( Although this part would belong to forum216: Javascript, if you need further assistance with the PHP part too, then better continue here. )
Feherke.
feherke.github.io
RE: Unselected Listbox values
That said you could choose checkboxed items and name all of them with name="arrayname[]", that'll arrive in PHP as $_POST['arrayname'] as an array, which means checked choices arrive in $_POST['arrayname'].
It's then an advanced job to create a widget that instead of displaying checkbox items displays two lists of choices and selections based on the checked status and vice versa by their usage also manipulate these form elements, which are the real deal inputs later sent to the backend. And that's even more advanced Javascript and CSS, most likely. But you may live with that kind of interface as long as you want to concentrate on learning the PHP side and simply rely on the fact it's possible to return an array of values from an HTML form, which is indeed possible.
What's perhaps most advisable would be learning a bit of jQuery as far as being able to override the normal HTML form submit with an event handler function using ajax to submit the form. You'll learn something useful in any case and gain more control about how the data of the HTML inputs are put together for your PHP side code as GET or POST request.
Plus you get an understanding about the HTTP protocol, which you should actually also have as a basis of how this all comes together anyway, and why the PHP side variables are called $_GET and $_POST at all.
Bye, Olaf.
Olaf Doschke Software Engineering
https://www.doschke.name
RE: Unselected Listbox values
I will be back in the real world soon, and will upload a synopsis of what I have done so far.
Basically I am using some JQuery I found online to control the 2 listboxes and the buttons that move items between them. I must admit I don't understand how it works yet - but that side works great!
I have tried a JQuery function that is supposed to select all items in the listbox whose id is passed to it, but it seems to select all items in both listboxes, but I have been unable to program PHP code to extract the selected line item ids, which is what I need to do. I wonder if I am getting the select id and name mixed up.
I have been wondering if a better option was to have a hidden input, with a Javascript/JQuery function thast is triggered when the submit button is clicked that gets all the lineitem ids from the required listbox, add them as an array to the input, which I assume must have an id (or name?) like "hidden[]" and then in the PHP code explode the array to get the individual ids that are then written out to the table.
Just need to work out how to do this.
Thanks
Roger
RE: Unselected Listbox values
At the top (before the HTML tag
CODE --> PHP
The Form part of the body
CODE --> PHP)
Below the </body> tag is the following Javascript
CODE --> Javascript/Jquery
What I have been trying to do is:
1) In the form include a hidden text input
2) Create an onselect script to extract all the IDs of the second selects options as comma separated variable that is then written to the hidden text.
3) In the $_POST of the PHP section:
Delete all existing records for the selected class
Explode the array in the Hidden input
For each mem_ID insert the class_id and mem_id into the table.
If I manually write the data to the hidden input I can get the PHP side to work ok. i.e. If I used value="1104,1328,787,1002" whthe Table would end up with records for those 4 values.
It's the onselect script and inserting it into the hidden input I am having problems with.
Cheers
Roger
RE: Unselected Listbox values
Function to create string of all items in listbox:
CODE --> JQuery
and the PHP code in the page that does the work!
CODE --> PHP