I have a form which has a dropdown list, its values coming from a mysql database. Values show fine. One option is the 'other'-value, which, if selected, should bring up an empty text box where the user can fill in whatever they want.
This form should when loaded at first come up with previously filled in values that come from the database (it's part of your regular edit-profile-form).
With the code below, the page loads fine with the 'old' data in the dropdown, but when the user wants to update it with a new value, the onChange-function doesn't take this new value, but sticks to the 'old' value, whichever select-option I take.
What am I missing here?
PS. I'd like not to have to refresh the page for this to work..
Hope it's a bit clear - thanks for anyone trying to help me out!
Code: Select all
function jobtitle_other(sel) {
alert(document.getElementById("jobtitle").value);
if(sel.value=="Other") {
document.getElementById("title_other").style.display = "inline";
}
else {
document.getElementById("title_other").style.display = "none";
}
}<form method="post" name="editprofile1" action="<?php echo $_SERVER['PHP_SELF']; ?>">
....
<label for="jobtitle">Job Title:</label>
Code: Select all
<?php
$query = "SELECT jobtitle from tbl_dd_jobtitle";
$result = mysqli_query($dbc, $query);
echo "<select name=\"jobtitle\" value=\"$jobtitle\" onChange=\"jobtitle_other(this)\">Title";
while ($row = mysqli_fetch_array($result)) {
$value = $row["jobtitle"];
$selected = (isset($jobtitle)&&$jobtitle==$value)? ' selected="selected"':'';
echo("<option value=\"$jobtitle\"$selected>$value</option>");
}
echo "</select>";
?><input name="title_other" type="text" style="display: none;"><br />