1

So, i finally got some results with ajax hyperlink -> query to DB.

And now i can see, the console, XHR admin-ajax.php post, with right results in jquery.

Jquery code.Problem is, that anything i do, it does not work.

jQuery(document).ready( function($) {
 $(".dropdown-item").click( function() {
    var data = { action: 'get_data',
      id: $(this).attr('id')
    };
$.ajax({
    url: "/wp-admin/admin-ajax.php",
    data: data,
    action: 'get_data',
    method: 'POST',
    dataType: 'html',    
    function(response) {
        $('.showPerson').html(response);
    }
      });

  });

});

get_Data function in function.php

function get_data() {
    global $wpdb;

    $o=$_POST['id'];
    $result2 = $wpdb->get_results("SELECT `firstname`, `lastname`, `department`, `phone`,`job_title`, `cell_phone`, `image`, `email`, `address`, `room`FROM employee WHERE department_id=" . $o );
//  foreach($result2 as $r){
//  echo  '<div class="info-block bg-grey m-b-2 p-a-2">';
//                  echo '<h4 class="h4 underline-sm">' . $r['firstname'] . " " . $r['lastname'] . " </h4>";
//                  echo '<div class="col-sm-8 text-xs-left">';
//                  echo '  <p class="m-b-1"><b>' .  $r['job_title'] ;
//                  echo        'Tööruum: ' . $r['room']; 
//                  echo    '</b></p>';
//                  echo    '<a href="mailto:' . $r['email'] . '">' .  $r['email'] . '></a>';
//                  echo    '<p class="m-b-0">' . $r['phone'] . " " . $r['cell_phone'] . '</php></p>';
//                  echo '</div>';
//                  echo '<div class=" col-sm-4 text-xs-right m-b-1">';
//                  echo  ' <img src="' .  $r['image'] . '" class="rounded-circle lg" alt="">';
//                  echo '</div>';
//              echo '</div>';
//          echo '</div>';
    //}


    echo json_encode($result2);

    wp_die();

}


add_action( 'wp_ajax_nopriv_get_data', 'get_data' );
add_action( 'wp_ajax_get_data', 'get_data' );

function my_enqueue() {
      wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/getData.js', array('jquery') );
      $data = array(
    'ajax_url' => admin_url( 'admin-ajax.php' )
);
 }
 add_action( 'wp_enqueue_scripts', 'my_enqueue' );

What i'm doing wrong? I can see the data - but i cant do anything with it?

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.