0

I'm pulling my data from the database and then I want to load the pulled data with ajax but I am not able to do so.

Following is my code.

jQuery code:

 jQuery(document).ready(function () {
   jQuery('#il').change(function () {
      let il_id = jQuery(this).val();
      jQuery.ajax({
        type: "POST",
        url: "../wp-content/themes/fancy-lab/ajax.php",
        data: {il: il_id},
        success: function (e) {
        jQuery('#ilce').show();
        jQuery('#ilce').html(e);
                }
            });
        });
});

ajax.php code:

global $wpdb;

$listele=$wpdb->get_results("Select * From wp_ilce where il_id={$_POST['il']}");

foreach ($listele as $val){
    echo "<option value='$val->id'>".$val->ilce."</option>";
}

But I keep getting an error like this get_results returns null. So where is the problem?

Ranjul Arumadi
  • 109
  • 1
  • 2
  • 11
Ferhat
  • 113
  • 1
  • 1
  • 9
  • First check print_r($_POST['il] so you know there is a value in the $_POST variable. See that post https://stackoverflow.com/questions/43557755/how-to-call-ajax-in-wordpress to include a Ajax in Wordpress. Maybe this will help you. Outside of your echo – Bernhard Beatus Aug 12 '21 at 14:35
  • I looked at the content of the link you sent, but I'm not looking for such a thing, I tried it in the normal php file, so it works there without any problems, it does not belong to woocommerce. But when I enter wordpress, I get confused. – Ferhat Aug 12 '21 at 14:43
  • You can try printing `var_dump("Select * From wp_ilce where il_id={$_POST['il']}")` to see the generated SQL query... if it is expected query or not! that will you give much visibility in the debug – Sajjad Hossain Sagor Aug 12 '21 at 16:11
  • Does anyone know the friends that I could not solve? :\ – Ferhat Aug 13 '21 at 05:22

0 Answers0