首页 > 解决方案 > 在搜索输入字段中获取 db mysql 值并在下拉列表中进行修改

问题描述

我找不到我的问题的解决方案,因为对我来说它是高级编程水平。我有一个自定义搜索字段,但我需要在下拉菜单中“转换它”,以便在 mysql 中获取一些用户值,避免编写数百个选择选项。这是我正在处理的注册表单字段

<tr class="user-luogo-wrap">
<th><label for="luogo">
            Luogo:  </label></th>
    <td><input type="text" name="luogo" id="luogo" value="Treviso" class="regular-text"></td>
    </tr>

用函数创建

function my_user_contactmethods( $user_contactmethods ){
$user_contactmethods['luogo'] = 'Luogo:';
return $user_contactmethods;
}
add_filter('user_contactmethods', 'my_user_contactmethods', 5);

这是我需要获取 'luogo' mysql 值并在下拉列表中修改它的字段

<div class="um-search-filter um-text-filter-type "> <input type="text" autocomplete="off" id="luogo" name="luogo" placeholder="Luogo" value="" class="um-form-field" aria-label="Luogo"></div>

我希望我已经解释清楚了。有人能帮我吗?

标签: javascriptphpmysqldatabaseforms

解决方案


在控制台中,我找到了表单数据,搜索操作后生成的请求有效负载: directory_id=3f9fc&page=1&search=&sorting=display_name&gmt_offset=8&post_refferer=61&nonce=f47827a450&luogo=boston&action=um_get_members

所以我试图修改这个功能

function field_choices( $field ) {  
    // reset choices
    $field['luogo'] = array();   
    // get the textarea value from options page without any formatting
    $choices = get_field('my_select_values', 'option', false);
    // explode the value so that each line is a new array piece
    $choices = explode("\n", $choices);   
    // remove any unwanted white space
    $choices = array_map('trim', $choices);
    // loop through array and add to field 'choices'
    if( is_array($choices) ) {      
        foreach( $choices as $choice ) {           
            $field['luogo'][ $choice ] = $choice;           
        }       
    }   
    // return the field
    return $field;    
}
add_action('um_get_members', 'field_choices');

我的直觉正确吗?


推荐阅读