[英]How to retrieve the selected value from dropdown and display it in the same dropdown box
[英]How to display selected value from dropdown in wordpress
我有状态表具有state_id和state_name。 我直接在如下所示的模板文件中显示它。
<select name="state" id="state" class="select-submit2">
<option value="">Select state</option>
<?php
$result=$wpdb->get_results("select * from states");
foreach($result as $row) {
$state_id=$row->state_id;
$state_name=$row->state_name;
echo '<option value='.$state_id.'>'.$state_name.'</option>';
}
?>
</select>
但是,当我要编辑时,如何显示第一个选定的州名。
编辑页面网址... whitecode.in/demo/plotsup_plot/new-property/?listing_edit=6795
这是我在function.php文件中的功能代码
function getcity(){
global $wpdb;
if($_POST['state'])
{
$id=$_POST['state'];
$property_id = $_GET['listing_edit'];
$district = get_post_meta($property_id, district, true);
$result=$wpdb->get_results("SELECT * FROM districts WHERE state_id='$id'");
//$wpdb->get_results($query);
foreach($result as $row) {
$city_name = $row->district_name;
$city_id = $row->district_id;
?>
<option value="<?php echo $city_id; ?>" <?php if($district == $city_id){ echo
'selected="selected"';} ?>><?php echo $city_name; ?></option>
<?php
//echo '<option value="'.$city_id.'">'.$city_name.'</option>';
}
}
}
按照前面的评论中的讨论,我猜该属性是您网站中的自定义帖子类型。 因此,如果是这种情况,那么这应该是解决方案。
<select name="state" id="state" class="select-submit2">
<option value="">Select state</option>
<?php
$property_id = $_GET['listing_edit']; //the PROPERTY_ID should be replaced with the original id might be ina get variable or any process by which you are using for the edit page.
$property_state = get_post_meta($property_id, META_KEY_STATE, true); //META_KEY_STATE is the meta_key name you use to store the value of the state in the postmeta table
$result=$wpdb->get_results("select * from states");
foreach($result as $row) {
$state_id=$row->state_id;
$state_name=$row->state_name;
?>
<option value="<?php echo $state_id; ?>" <?php if($property_state == $state_id){ echo 'selected="selected"';} ?>><?php echo $state_name; ?></option>
<?php
}
?>
</select>
试试这个,让我知道您是否遇到任何问题。
如果这是您想要的?
<select name="state" id="state" class="select-submit2">
<option value="">Select state</option>
<?php
$result=$wpdb->get_results("select * from states");
foreach($result as $row) {
$state_id=$row->state_id;
$state_name=$row->state_name;
if($state_id == SELECTED_STATE_ID)
echo '<option value='.$state_id.' selected>'.$state_name.'</option>';
else
echo '<option value='.$state_id.'>'.$state_name.'</option>';
}
?>
</select>
只需将SELECTED_STATE_ID替换为包含选择了哪个状态的变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.