$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Displaying list of cities according to the selected state. Using the WordPress hook|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Displaying list of cities according to the selected state. Using the WordPress hook

matteradmin9PV0评论

I come to ask the community a help of a simple hook on WordPress. I'm trying to display a list of states and cities in two dropdows to select a single option in the a form. The list of cities will be displayed according to the selected state.

But the dropdown of list city is returning the source code of site. I do not know why this is happening, so I ask the help.

Here the code added in the Functions of theme:

<script type="text/javascript" src=".6.0.3.js"></script>

    <select name="state" id="state" onchange="loadCity(this)">

    <option>-- Select the State --</option>

            <?php

global $wpdb;

//get the list of states

$list_of_state = ("SELECT DISTINCT meta_value FROM $wpdb->usermeta WHERE meta_key = 'state_acf_subscriber' ORDER BY meta_value ASC");

$option_of_state = mysql_query($list_of_state);

$total_of_state = mysql_num_rows($option_of_state);

            for($i = 0; $i < $total_of_state; $i++){

                    $state = mysql_fetch_array($option_of_state);

                        if($city[meta_value] != ''){

                                 echo "<option value=\"". $state['meta_value']."\">".$state['meta_value']."</option>";
                         }
            }?>

    </select>

<script type="text/javascript">
function loadCity(meta_value){

if(meta_value){
    var myAjax = new Ajax.Updater('listCity','listcity.ajax.php',
    {
        method : 'get',
    });
}
}
</script>


<div id="listCity"><label for="City">City: </label>

    <select name="city" id="listOfCity">
        <option value="">Select the City</option>
    </select>
</div>

Here the another file:

<?php

global $wpdb;
//get the state selected on the dropdown of states
$state_selected = $_GET['meta_value'];

//below, get the list of city according the user's id what's are associated with state selected

$list_of_city = "SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = 'city_acf_subscriber'
             AND user_id IN (SELECT DISTINCT user_id FROM $wpdb->usermeta WHERE meta_key = 'state_acf_subscriber' AND meta_value = '".$state_selected."') ORDER BY meta_value";

$option_of_city = mysql_query($list_of_city);

$total_of_city = mysql_num_rows($option_of_city);

for($j = 0; $j < $total_of_city; $j++){

$city = mysql_fetch_array($option_of_city);

echo "<option value=\"". $city['meta_value']."\">" . $city['meta_value'] ."</option>";

}

The list of states is correct, the dropdown appears showing all states added by subscribers. I just can not view the list of cities

Each user (subscriber) will select your state and city on your profile, and what I need is to be able to select the state and city to filter a list of users on the frontend.

I appreciate any help.

Post a comment

comment list (0)

  1. No comments so far