Reputation: 115
This is an odd one. I have, with the help of others and sample code and so on, put together a routine that, based on one select, via ajax, passes the value to a php file, which then builds a second select with options, each option has the code set to check the value of a variable and set the selected attribute. The following is an example of the returned select statement with options:
<select class='form-control' id='branch' name='branch'>
<option value = '' selected></option>
<option value="Caldarium"<?php if( $branch=="Caldarium" ) echo ' selected' ?>Caldarium</option>
<option value="Cloondara"<?php if( $branch=="Cloondara" ) echo ' selected' ?>Cloondara</option>
<option value="Crosston"<?php if( $branch=="Crosston" ) echo ' selected' ?>Crosston</option>
<option value="Darkwood"<?php if( $branch=="Darkwood" ) echo ' selected' ?>Darkwood</option>
<option value="Darkwood - Caer Darth"<?php if( $branch=="Darkwood - Caer Darth" ) echo ' selected' ?>Darkwood - Caer Darth</option>
<option value="Darkwood - Hawks Haven"<?php if( $branch=="Darkwood - Hawks Haven" ) echo ' selected' ?>Darkwood - Hawks Haven</option>
<option value="Darkwood - Konigstadt"<?php if( $branch=="Darkwood - Konigstadt" ) echo ' selected' ?>Darkwood - Konigstadt</option>
<option value="Darkwood - Montaigne du Roi"<?php if( $branch=="Darkwood - Montaigne du Roi" ) echo ' selected' ?>Darkwood - Montaigne du Roi</option>
<option value="Darkwood - St. David's"<?php if( $branch=="Darkwood - St. David's" ) echo ' selected' ?>Darkwood - St. David's</option>
<option value="Esfenn"<?php if( $branch=="Esfenn" ) echo ' selected' ?>Esfenn</option>
<option value="Southern Shores"<?php if( $branch=="Southern Shores" ) echo ' selected' ?>Southern Shores</option>
<option value="St. Katherine"<?php if( $branch=="St. Katherine" ) echo ' selected' ?>St. Katherine</option>
<option value="Teufelberg"<?php if( $branch=="Teufelberg" ) echo ' selected' ?>Teufelberg</option>
<option value="the Mists"<?php if( $branch=="the Mists" ) echo ' selected' ?>the Mists</option>
<option value="Vinhold"<?php if( $branch=="Vinhold" ) echo ' selected' ?>Vinhold</option>
<option value="Westermark"<?php if( $branch=="Westermark" ) echo ' selected' ?>Westermark</option>
<option value="Wolfscairn"<?php if( $branch=="Wolfscairn" ) echo ' selected' ?>Wolfscairn</option>
</select>
In theory there should be a closing bracket after the question mark for the PHP (?>), but it was showing up in the string.
Anyway, if I copy this code directly into the HTML, everything works as expected. If I have it stuffed into a DIV tag's html via a command in JavaScript like:
$("#namebranchoptions").html(data);
The contents appear correctly in the select, but nothing is showing as selected (it shows as blank). I have inserted a statement into the HTML after this to show the value of the variable:
<?php echo "Branch: " . $branch . "<br />" ?>
And that appears correctly. I am stumped. As noted, if I paste the above into the HTML, into the DIV tag manually and re-load the page, I get the expected results. I cannot see why there is this difference. It may be some subtle aspect of the SELECT or something, I don't know. It's ... frustrating.
Upvotes: 1
Views: 54
Reputation: 115
Thanks for the suggestions. After puzzling a bit, I worked out how to handle it ... as was pointed out by the first comment, the PHP program should be determining the option to be marked as "select"ed ... the trick then became referencing the branch that I needed to do the comparison (what the value was to determine which option had the select attribute set). I used an input set as hidden:
<input id="namebranch" value = "<?php echo $branch; ?>" hidden />
Then modified the ajax code:
function getNameBranch()
{
var region = document.getElementById("nameregion").value;
var branch = document.getElementById("namebranch").value;
$.ajax
({
type: "POST",
url: "namebranch.php",
data: { 'myRegion' : region, 'branch' : branch },
//cache: false,
success: function(data)
{
//alert( data );
$("#namebranchoptions").html(data);
} // end success
}); // end ajax call
}; // end function getNameBranch
After tinkering with the PHP a bit to ensure I had the right options it worked perfectly. Thanks for pointing me in the right direction!
Upvotes: 1
Reputation: 2083
Won't you have 2x elements with selected if you do this? <option value = '' selected></option>
? The first option will be selected by default even without the selected
attribute unless the selected attribute is specified on a sibling.
It's missing a closing >
on each of your opening option-tags which could be causing difficulty. Right after echo ' selected' ?>
, you're closing the <?php ### ?>
but not the html-tag.
<select class='form-control' id='branch' name='branch'>
<option value = '' selected></option>
<option value="Caldarium"<?php if( $branch=="Caldarium" ) echo ' selected' ?>>Caldarium</option>
<option value="Cloondara"<?php if( $branch=="Cloondara" ) echo ' selected' ?>>Cloondara</option>
<option value="Crosston"<?php if( $branch=="Crosston" ) echo ' selected' ?>>Crosston</option>
<option value="Darkwood"<?php if( $branch=="Darkwood" ) echo ' selected' ?>>Darkwood</option>
<option value="Darkwood - Caer Darth"<?php if( $branch=="Darkwood - Caer Darth" ) echo ' selected' ?>>Darkwood - Caer Darth</option>
<option value="Darkwood - Hawks Haven"<?php if( $branch=="Darkwood - Hawks Haven" ) echo ' selected' ?>>Darkwood - Hawks Haven</option>
<option value="Darkwood - Konigstadt"<?php if( $branch=="Darkwood - Konigstadt" ) echo ' selected' ?>>Darkwood - Konigstadt</option>
<option value="Darkwood - Montaigne du Roi"<?php if( $branch=="Darkwood - Montaigne du Roi" ) echo ' selected' ?>>Darkwood - Montaigne du Roi</option>
<option value="Darkwood - St. David's"<?php if( $branch=="Darkwood - St. David's" ) echo ' selected' ?>>Darkwood - St. David's</option>
<option value="Esfenn"<?php if( $branch=="Esfenn" ) echo ' selected' ?>>Esfenn</option>
<option value="Southern Shores"<?php if( $branch=="Southern Shores" ) echo ' selected' ?>>Southern Shores</option>
<option value="St. Katherine"<?php if( $branch=="St. Katherine" ) echo ' selected' ?>>St. Katherine</option>
<option value="Teufelberg"<?php if( $branch=="Teufelberg" ) echo ' selected' ?>>Teufelberg</option>
<option value="the Mists"<?php if( $branch=="the Mists" ) echo ' selected' ?>>the Mists</option>
<option value="Vinhold"<?php if( $branch=="Vinhold" ) echo ' selected' ?>>Vinhold</option>
<option value="Westermark"<?php if( $branch=="Westermark" ) echo ' selected' ?>>Westermark</option>
<option value="Wolfscairn"<?php if( $branch=="Wolfscairn" ) echo ' selected' ?>>Wolfscairn</option>
</select>
The code would be cleaner if you loop over an array. like:
$things = array(
array('name'=>'Caldarium'),
... etc..
);
$element = '<select class='form-control' id='branch' name='branch'>';
$element = '<option value=""></option>';
foreach ($things as $thing){
$selected='';
if($thing['name']===$branch){
$selected="selected"
}
$element .= '<option value="'.$thing['name].'" '.$selected.'>'.$thing['name].'</option>';
}
$element .= '</select>
echo $element
Upvotes: 1