Populate list of options with Ajax - Replacing Prototype

Populate list of options with Ajax - Replacing Prototype

HI, I have a problem with a jQuery code.

Actually, I want to migrate from Propotype due to conflicts with other jQuery scripts.

I need display a list of options in a select from a previously option selected in another select.

I've tried several ways, but without success.

I added two fields to the users profile: City and Neighborhood. So, a form in the frontend to be able to search for users by city and neighborhood (of each city).

Here's my code:

The file  neighborhoodAjax.php

  1. <?php
  2. /*
  3.  This file is used to search the neighborhoods of the city selected in the first select and populate the list of neighborhoods of that city.
  4. */

  5. global $wpdb; // here's used for connect in DB and get the prefix of table

  6. if($_POST['meta_value']){

  7. $city_selected = $_POST['meta_value'];

  8. $neighborhood = "SELECT DISTINCT meta_value FROM $wpdb->usermeta WHERE meta_key = 'neighborhood_of_user' AND user_id IN (SELECT DISTINCT user_id FROM $wpdb->usermeta WHERE meta_key = 'city_of_user' AND meta_value = '".$city_selected."')";

  9. $neighborhood = sanitize_text_field($neighborhood);

  10. $neighborhood = mysql_query($neighborhood); ?>

  11.         <option value="" selected="selected">-- Select a Neighborhood --</option>

  12. <?php while($neighborhood = mysql_fetch_array($neighborhood)){ ?>

  13.         <option value="<?php echo $neighborhood['meta_value'] ?>" <?php if($_POST['select_neighborhood'] == $neighborhood['meta_value']) {?> selected="selected" <?Php } ?>> <?php echo $neighborhood['meta_value']?> </option>

  14. <?php }//Close the While

  15. }//Close the if
  16. ?>

The Script: 

  1. <script type="text/javascript">

  2. $(function() {

  3.   $("#city").bind("change", function() { // #city is ID of select

  4.      var meta_value = $("#city").val();
  5.         $.ajax({
  6.           url: "neighborhoodAjax.php",
  7.           type: "POST",
  8.           data: {id : meta_value},
  9.           dataType: "html"
  10.         });

  11.   });

  12. });

  13. </script>

The code will run in WordPress website, what I would like to know is where the error is. :(

Can anyone help me?

I am grateful.