0

Ok so I am trying to chain select boxes from a my database. First select box grabs the values just fine. If I hard code all of the select boxes they all return values and as expected do not interact with each other. The problem I am having is the external call to my holder php file for the second drop down. jquery posts to the file and it returns the error I put in the title of this post. I am used to this error showing when $wpdb is not set to global which but it does not seem to be the case as I have declared the global $wpd; in several places trying to debug the code. Here is my front end code

include_once 'select.php';
global $wpdb;
?>
<script type="text/javascript">
    $(document).ready(function(){
        $("select#make").attr("disabled","disabled");
        $("select#year").change(function(){
        $("select#make").attr("disabled","disabled");
        $("select#make").html("<option>wait...</option>");
        var year = $("select#year option:selected").attr('value');
        $.post("<?php bloginfo('template_url');?>/select_type.php"{year:year},         function(data){
            $("select#type").removeAttr("disabled");
            $("select#type").html(data);
        });
    });
    $("form#select_form").submit(function(){
        var cat = $("select#year option:selected").attr('value');
        var type = $("select#make option:selected").attr('value');
        if(cat>0 && type>0)
        {
            var result = $("select#make option:selected").html();
            $("#result").html('your choice: '+result);
        }
        else
        {
            $("#result").html("you must choose two options!");
        }
        return false;
    });
});
</script>

<div id="content" class="account-content-area">

        <div class="my_box3">

            <form id="select_form">
    Choose a category:<br />
    <select id="year">
        <?php echo $opt->getYear(); ?>
    </select>
    <select id="make">

    </select>
    <select id="model">

    </select>
    <select id="trim">

    </select>
    <br /><br />
    <input type="submit" value="confirm" />

here is select.php with line 26 indicated

class selectList {

public function getModel(){
    global $wpdb;
    $sql = $wpdb->get_results(
        "

        SELECT DISTINCT model

        FROM wp_car_details 

        ORDER BY model DESC

        "
        );

    foreach ($sql as $res) {
    echo '<option value="'.$res->model.'">'.$res->model.'</option>';
    }
}

public function getMake(){
    global $wpdb;
    $sql2 = $wpdb->get_results( <---- line 26 ----->
        "

        SELECT *

        FROM wp_car_details

        WHERE make
        "
        );

    foreach ($sql2 as $res2) {
        echo '<option value="'.$res2->make.'">'.$res2->make.'</option>';
    }

}

public function getTrim(){
    global $wpdb;
    $sql3 = $wpdb->get_results(
        "

        SELECT *

        FROM wp_car_details

        WHERE trim

        "
        );

    foreach ($sql3 as $res3) {
        echo '<option value="'.$res3->trim.'">'.$res3->trim.'</option>';
    }

}

public function getYear(){
    global $wpdb;
    $sql4 = $wpdb->get_results(
        "

        SELECT DISTINCT year

        FROM wp_car_details

        ORDER BY year DESC

        "
        );

    $year = '<option value="0">choose...</option>';
    foreach ($sql4 as $res4) {
    $year = '<option value="'.$res4->year.'">'.$res4->year.'</option>';
    echo $year;
    }

}

}

$opt = new selectList();

Here is select_type.php

global $wpdb;
include_once 'select.php';
echo $opt->getMake();

I fully understand that my approach is probably the wrong way to go about this whole ordeal but this is my first time putting this together so be kind please. :P

my question is why is the $_POST to select_type.php returning the fatal error and what can I do to correct it?

Also please advise if I have not included enough information I included what I thought was relevant for the initial question.

  • If this code lives in an external file, then I think WordPress might not now about it, thus the error. – birgire Aug 4 '14 at 22:49
  • firebug says select_type.php is found and loaded properly. Thanks for the reply! – Nathan Aug 4 '14 at 22:54
  • yes, but where do you load WordPress in this external select_type.php file? Did you check out the Codex on ajax in plugins? – birgire Aug 4 '14 at 22:57
  • 1
    I have not added a wp_ajax call. I'll spend some looking over the codex and learn a little more about it. – Nathan Aug 4 '14 at 23:23
  • Here's also a Codex page that might be helpful loading WordPress to an external file (by using only two lines of code!), but I would study the wp_ajax way. Good luck. – birgire Aug 5 '14 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.