我有一个select html标记。我需要显示选项值取决于输入文本中的职务名称。例如,如果职务名称是会计,则将在选项值中显示会计位置列表。
但我的代码不起作用。请帮我解决这个问题:
<select name="locations_list" id="filterbyhiringlocation">
<option name="default" class="filter_by" selected="selected" value="Select by Location">Select by Location</option>
<?php
if(isset($_POST['jobTitle']) && $_POST['jobTitle'] == 'Sales Associate'){
foreach($query_hiring_location_mass as $option){
echo '<option name="locations_list" class="filter_by" selected value="'. $option->hiring_location .'">'. $option->hiring_location .'</option>';
} else {
echo '<option name="locations_list" class="filter_by" value="'. $option->hiring_location.'">'. $option->hiring_location .'</option>';
}
} else if(isset($_POST['jobTitle']) && $_POST['jobTitle'] == 'District Manager'){
foreach($query_hiring_location_non_mass as $option){
echo '<option name="locations_list" class="filter_by" selected value="'. $option->hiring_location .'">'. $option->hiring_location .'</option>';
} else {
echo '<option name="locations_list" class="filter_by" value="'. $option->hiring_location.'">'. $option->hiring_location .'</option>';
}
}
?>
</select>错误:
Parse error: syntax error, unexpected 'else' (T_ELSE)发布于 2016-05-24 11:17:06
您的代码应该如下所示:
<?php
$hiring_location = 'YOUR DEFAULT LOCATION';
?>
<select name="locations_list" id="filterbyhiringlocation">
<option name="default" class="filter_by" selected="selected" value="Select by Location">Select by Location</option>
<?php
if(isset($_POST['jobTitle']) && $_POST['jobTitle'] == 'Sales Associate'){
if(count($query_hiring_location_mass)) {
foreach($query_hiring_location_mass as $option){
echo '<option name="locations_list" class="filter_by" selected value="'. $option->hiring_location .'">'. $option->hiring_location .'</option>';
}
}
else {
echo '<option name="locations_list" class="filter_by" value="'. $hiring_location.'">'. $hiring_location .'</option>';
}
} else if(isset($_POST['jobTitle']) && $_POST['jobTitle'] == 'District Manager'){
if(count($query_hiring_location_non_mass)) {
foreach($query_hiring_location_non_mass as $option){
echo '<option name="locations_list" class="filter_by" selected value="'. $option->hiring_location .'">'. $option->hiring_location .'</option>';
}
}
else {
echo '<option name="locations_list" class="filter_by" value="'. $hiring_location.'">'. $hiring_location .'</option>';
}
}
?>
</select>发布于 2016-05-24 11:20:18
您的代码应该如下所示:
<select name="locations_list" id="filterbyhiringlocation">
<option name="default" class="filter_by" selected="selected" value="Select by Location">Select by Location</option>
<?php
if(isset($_POST['jobTitle']) && $_POST['jobTitle'] == 'Sales Associate'){
foreach($query_hiring_location_mass as $option){
if ($option->hiring_location == $yourLocation) {
echo '<option name="locations_list" class="filter_by" selected value="'. $option->hiring_location .'">'. $option->hiring_location .'</option>';
} else {
echo '<option name="locations_list" class="filter_by" value="'. $option->hiring_location.'">'. $option->hiring_location .'</option>';
}
}
} else if(isset($_POST['jobTitle']) && $_POST['jobTitle'] == 'District Manager'){
foreach($query_hiring_location_non_mass as $option){
if ($option->hiring_location == $yourLocation) {
echo '<option name="locations_list" class="filter_by" selected value="'. $option->hiring_location .'">'. $option->hiring_location .'</option>';
} else {
echo '<option name="locations_list" class="filter_by" value="'. $option->hiring_location.'">'. $option->hiring_location .'</option>';
}
}
}
?>
</select>在这里,$yourLocation应该是按照您的需求,即,您想要显示选定的
发布于 2016-05-24 11:25:38
@user,我想您错过了两个花括号来关闭循环,请使用以下代码:
if(isset($_POST['jobTitle']) && $_POST['jobTitle'] == 'Sales Associate'){
foreach($query_hiring_location_mass as $option){
echo '<option name="locations_list" class="filter_by" selected value="'. $option->hiring_location .'">'. $option->hiring_location .'</option>';
}
} else {
echo '<option name="locations_list" class="filter_by" value="'. $option->hiring_location.'">'. $option->hiring_location .'</option>';
}
} else if(isset($_POST['jobTitle']) && $_POST['jobTitle'] == 'District Manager'){
foreach($query_hiring_location_non_mass as $option){
echo '<option name="locations_list" class="filter_by" selected value="'. $option->hiring_location .'">'. $option->hiring_location .'</option>';
}
} else {
echo '<option name="locations_list" class="filter_by" value="'. $option->hiring_location.'">'. $option->hiring_location .'</option>';
}
}https://stackoverflow.com/questions/37411776
复制相似问题