dongxia1390
2016-07-27 06:28
浏览 97

Wordpress ajax数据库调用

I am struggling with getting ajax to work, if someone can guide me, it'd be greatly appreciated.

I am building this as a plugin.

I have a file named courselinkscript.js and contains this

jQuery(document).ready(function($){

jQuery(".courselist").change(function () {

    jQuery.ajax({
            type:"POST",
            url: my_ajax_object.ajax_url,
            data: { 'action': 'getLinkedCourses' }
            //where/what do I put here to work with the data I received. 
    })


    });
});

Then in my main php file named course-listing.php I have this

function getLinkedCourses() {
    global $wpdb;
    $results = $wpdb->get_results( 'SELECT list.ID, list.course, list.cost, list.length, link.CourseID FROM `wp_course_list` AS list INNER JOIN `wp_course_link` as link ON (list.ID=link.LinkID) WHERE link.CourseID = 1', OBJECT );
    echo json_encode($results);

wp_die();

}


function wpb_adding_scripts() {
wp_register_script('courselinkscript', plugins_url('courselinkscript.js', __FILE__), array('jquery'),'1.0', true);
wp_enqueue_script('courselinkscript');
wp_localize_script( 'courselinkscript', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}

add_action( 'wp_ajax_my_list', 'getLinkedCourses' );

add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );  

My calls to ajax don't work as I don't understand what needs to be done to initalise it. Any help is appreciated.

图片转代码服务由CSDN问答提供 功能建议

我正在努力让ajax工作,如果有人可以指导我,我会非常感激。

我正在将其构建为插件。

我有一个名为courselinkscript.js的文件并包含此

   jQuery(document).ready(function($){
 
jQuery(“。courselist”)。change(function(){
 
 jQuery.ajax({
 type:“POST”,\  n url:my_ajax_object.ajax_url,
 data:{'action':'getLinkedCourses'} 
 //在哪里/我在这里处理我收到的数据。
})
 
 
}  ); 
}); 
   
 
 

然后在我的主要php文件中命名为course-listing.php我有这个

   function getLinkedCourses(){
 global $ wpdb; 
 $ results = $ wpdb-> get_results('SELECT list.ID,list.course,list.cost,list.length,link.CourseID FROM`wp_course_list  `AS列表INNER JOIN`wp_course_link`作为链接ON(list.ID = link.LinkID)WHERE link.CourseID = 1',OBJECT); 
 echo json_e  ncode($ results); 
 
 
wp_die(); 
 
} 
 
 
功能wpb_adding_scripts(){
wp_register_script('courselinkscript',plugins_url('courselinkscript.js',__ FILE__),array('jquery  '),'1.0',true); 
wp_enqueue_script('courselinkscript'); 
wp_localize_script('courselinkscript','my_ajax_object',array('ajax_url'=>  admin_url('admin-ajax.php'))); 
} 
 
add_action('wp_ajax_my_list','getLinkedCourses'); 
 
add_action('wp_enqueue_scripts','wpb_adding_scripts');  
   
 
 

我对ajax的调用不起作用,因为我不明白需要做什么才能使其失效。 任何帮助表示赞赏。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyinshua9996 2016-07-27 06:45
    已采纳

    Use the below mentioned code. It should work.

    function getLinkedCourses() {
       global $wpdb;
       $results = $wpdb->get_results( 'SELECT list.ID, list.course, list.cost, list.length, link.CourseID FROM `wp_course_list` AS list INNER JOIN `wp_course_link` as link ON (list.ID=link.LinkID) WHERE link.CourseID = 1', OBJECT );
       echo json_encode($results);
    
      wp_die();
    
    }
    
    
    function wpb_adding_scripts() {
       wp_register_script('courselinkscript', plugins_url('courselinkscript.js', __FILE__), array('jquery'),'1.0', true);
       wp_enqueue_script('courselinkscript');
       wp_localize_script( 'courselinkscript', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
    }
    
    add_action( 'wp_ajax_getLinkedCourses', 'getLinkedCourses' );
    add_action( 'wp_ajax_nopriv_getLinkedCourses', 'getLinkedCourses' );
    // add_action( 'wp_ajax_my_list', 'getLinkedCourses' );
    
    add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );
    

    Replace your javascript with the below code:

     jQuery(document).ready(function($){
    
       jQuery(".courselist").change(function () {
    
          jQuery.ajax({
             type:"POST",
             url: my_ajax_object.ajax_url,
             data: { 'action': 'getLinkedCourses' },
             //where/what do I put here to work with the data I received.
             success: function(data) {
                 var obj = jQuery.parseJSON(data);
                 console.log(obj);
             },
          });
    
       });
    }); 
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题