Charles Butler
Charles Butler

Reputation: 573

Get Text from selected value of a selectbox

Profile Page:

Users update their information:

Country is a select field that gets the option values and inserts it into the database, I also have a code that gets the selected TEXT and inserts that into the database.

col_country_code & col_country_name

This works fine when the selectbox is always being selected.

echo "<select name='$select_name' id='$select_name' onchange='document.getElementById(\"location_content\").value=this.options[this.selectedIndex].text'>";

This adds the selected option values TEXT into a hidden input. BUT it only works onchange...

What if a user only wants to update their Biography? Problem is, its not sending the option values TEXT because there is no onchange event!

How do I solve this problem? Right now if a user wants to update his/her profile they need to select their country every time? I dont want this...

Upvotes: 0

Views: 68

Answers (1)

Mostafa Talebi
Mostafa Talebi

Reputation: 9183

ok, what you should do is that, on page load you have to set the <select> value to a value already in DB, so that on save, if the user does not change the <select>, the value which was saved to DB gets saved, if changed, then the changed value gets saved.

something like this:

$selectValue = getCountry(); // this gets the information of the user from DB

echo "<select name='$select_name' id='$select_name' onchange='document.getElementById(\"location_content\").value=this.options[this.selectedIndex].text' value='$selectValue'>";

Note that I have added a new attribute (value)

Upvotes: 1

Related Questions