MySQL select query with ajax in wordpress

I am passing variable with ajax to list.php in my twentytwelve template. In list.php I am executing mysql select query but when I see in console I am getting this error:

<br />
<b>Fatal error</b>:  Call to a member function get_results() on a non-object in <b>D:xampphtdocswordpresswp-contentthemestwentytwelvelist.php</b> on line <b>4</b><br />

My Code:

Read More

search.php

<?php
/*
Template Name: Search
*/
get_header();?>

<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
  $("#parent_category").change(function()
  {
    var parent_category = $(this).val();
    if(parent_category != '')  
     {
      $.ajax
      ({
         type: "POST",
         url: "<?php echo get_template_directory_uri(); ?>/list.php",
         data: "parent_category="+ parent_category,
         success: function(option)
         {
           $("#child_category").html(option);
         }
      });
     }
     else
     {
       $("#child_category").html("<option value=''>-- No category selected --</option>");
     }
    return false;
  });
});
</script>

<select id="parent_category" name="parent_category">
    <option value="" selected="selected">-- Select blood group --</option>
    <option value="A1 positive">A1 positive</option>
    <option value="A1 negative">A1 negative</option>
    <option value="A2 positive">A2 positive</option>
    <option value="A2 negative">A2 negative</option>
    <option value="B positive">B positive</option>
    <option value="B negative">B negative</option>
    <option value="A1B positive">A1B positive</option>
    <option value="A1B negative">A1B negative</option>
    <option value="A2B positive">A2B positive</option>
    <option value="A2B negative">A2B negative</option>
    <option value="AB positive">AB positive</option>
    <option value="AB negative">AB negative</option>
    <option value="O positive">O positive</option>
    <option value="O negative">O negative</option>
    <option value="A positive">A positive</option>
    <option value="A negative">A negative</option>
</select>

<select id="child_category" name="child_category">
  <option value="">-- No location selected --</option>
</select>

<?php get_footer(); ?>

list.php

<?php
if(isset($_POST['parent_category']) && $_POST['parent_category'] != '')
{   
    $result = $wpdb->get_results( "SELECT home_location FROM wp_places WHERE blood_group LIKE '".$getGroupType."%'" );
    print_r($result);   
}
?>

Any ideas or suggestions? Thanks.

Related posts

Leave a Reply

1 comment

  1. you have to include the wp-blog-header in your list.php then define $wpdb as global the error you are is due to the definition of $wpdb which is undefined

    <?php include("yourpath/wp-blog-header.php");
     global $wpdb;
    if(isset($_POST['parent_category']) && $_POST['parent_category'] != '')
    {   
        $result = $wpdb->get_results( "SELECT home_location FROM wp_places WHERE blood_group LIKE '".$getGroupType."%'" );
        print_r($result);   
    }
    ?>