I have an HTML listbox, I want to be able to select any value in that listbox and update a second listbox with a PHP database query based on that selection.
Options I have tried:
-I have tried using JSON. I update the second listbox using Javascript and calling an external PHP script, but the returned PHP value would be an array and that wont convert well with Javascript.
-I am not able to use cookies, so I use HTML Sessions. When you select a value in the first listbox it updates a session value and refreshes the page. Upon reload, the page does a PHP script using the Session value to update the second listbox, but I cant update the HTML Session with the selected value of the first listbox using Javascript by calling onChange=”function(); with the first listbox”.
Is there any other available options to implement this? Or how can I fix one of these options to make this work?
You say that you’ve tried using JSON, but you clearly don’t completely understand what that means. JSON is simply a way of formatting strings that can be understood universally by any machine which has a parser for it. The idea that the PHP value is an “array” and therefore unconvertible makes no sense; all that matters is that it’s sent to the client in JSON format.
If you have an array in PHP, you can convert it to JSON format using the
json_encodefunction:When you receive it in JSON format in your JavaScript code, you can then parse it into a JavaScript object – see this question for details.