MySQL select query with ajax

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

1 comment

  1. Your list.php file does not load any of the WordPress Core, at least there is no indication that it does based on the code you posted. Thus, WordPress classes and functions are not going to be available, hence the error.

    Merely having a file in the theme folder does not mean that WordPress will load the file, or that when accessed the file will be loaded in a WordPress context.

    Do the right thing and use the AJAX API for AJAX requests. That is what it was meant for. You are doing this the hard way, and the painful way. There are plenty of examples in the Codex for using the AJAX API and plenty of questions here about it as well. Get started. If you have trouble, edit your question with the specifics.

Comments are closed.