views:

28

answers:

1

Hello I have a html select which has an onchange event. And whenever a user selects an option, data related to that option would be retreived from the database. But the select is set to its original or default option as a post would occur. How do I set the select to the user selected option??

I am using cakePHP for this, but I am pretty sure it is a PHP-Javascript thing.

+1  A: 

You can store the state of the select in a cookie and retrieve it on page load. Or retrieve the value on the server and either set it in a PHP file before you send the page to the client, or add a hidden element with that value and use that to set it on page load.

Robusto
I have it set in a hidden element, and I retreive that from the $_POST array. But I would only have the value of the chosen option and not the index. How would I set it to selected?
macha
The most basic way would be iterate through the select.options array matching the value of the hidden element to the value of each option, and exit when it matches. The value of the iterator at that point will be the index. Then set the selectedIndex property of your select element to that. Oh, and while you're at it, work on your accept rate.
Robusto