WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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="http://prototypejs.org/assets/2008/9/29/prototype-"></script>

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

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


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>";


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

    var myAjax = new Ajax.Updater('listCity','listcity.ajax.php',
        method : 'get',

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

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

Here the another file:


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.

share|improve this question
Hi, you'd better use WordPress API's to interact with your database see documnentation for class wpdb. There are get_row(), get_results() and get_var() you can use to retrieve datas. – JMau Jun 12 '13 at 13:40
Hello TweetPressFr, Thanks for the reply. I changed the functions of queries, but does not work. I ran the sql query in phpmyadmin and it returns the values ​​correctly. I think the error is with the javascript code. – enoquenroll Jun 12 '13 at 15:29
see this – JMau Jun 12 '13 at 15:40
Hi TweetPressFr, I will analyze the codex and see what is happening. Thanks for the tip. – enoquenroll Jun 13 '13 at 3:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.