【问题标题】:Admin-ajax is not working?Admin-ajax 不工作?
【发布时间】:2017-01-25 06:35:10
【问题描述】:

我正在后端创建一个学生表单类型插件来插入、更新、删除、列出数据。

一切都很顺利,直到我将数据保存到 db。我知道使用 $wpdb 类的所有进程。我曾使用 $wpdb 完成此任务,但现在我使用 admin-ajax 完成此任务。

我在我的主插件文件中学习了 admin-ajax 应用,它可以工作。 但我必须在我的另一个页面 st_db_process 调用 ajax。 我在哪里调用 ajax,但它给出 0 输出。

这是我的代码:

我的主要插件文件:

/*
Plugin Name: student management
Plugin URI: http://wordpress.org/plugins/student/
Description: This plugin is useful to add new student data,update data,delete data and list of all students.
Author: Jayendra Manek
Version: 1.0
Author URI:
*/

//Defining Constants
define( 'ST_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define('ST_PLUGIN_URL',plugin_dir_url(__FILE__));

define('ST_VIEW_PATH', plugin_dir_path( __FILE__ ).'view/' );
define('ST_VIEW_URL', plugin_dir_url( __FILE__ ).'view/' );

define('ST_CLASS_PATH', plugin_dir_path( __FILE__ ).'classes/' );
define('ST_CLASS_URL',plugin_dir_url(__FILE__).'classes/');

global $table_name;
$table_name=$wpdb->prefix . 'student_management';

global $student_management,$st_db_process;
$student_management=new student_management();

global $student_ajaxurl;
$student_ajaxurl = admin_url('admin-ajax.php');

class student_management
{

    function __construct()
    {
        //Plugin Activation Hook
        register_activation_hook(__FILE__, array($this,'st_plugin_setup'));

        //menu action and load script
        add_action('admin_menu', array($this,'st_create_menu'));
        add_action('admin_enqueue_scripts',array($this,'st_load_script'));
        add_action('admin_enqueue_scripts',array($this,'st_include_class_files'));

    }

    function st_include_class_files()
    {
        require_once ST_CLASS_PATH.'st_db_process.php';
    }

    //creating main-menu,sub-menu
    function st_create_menu()
    {
        add_menu_page('All Students', 'Student Manage', 'manage_options', 'main-menu', array($this,'st_list_page'));
        add_submenu_page('main-menu', 'Add new Student', 'Add New Student', 'manage_options', 'add-new-student',array($this,'st_add_new_student_page'));
        add_submenu_page(null, 'Add new Student', 'Add New Student', 'manage_options', 'sub-menu',array($this,'st_add_new_student_page'));
        add_submenu_page(null, 'Update Student', 'update data', 'manage_options', 'sub-menu-update',array($this,'st_update_data_page'));
    }

    //list page
    function st_list_page()
    {
        require_once ST_VIEW_PATH . 'list.php';
    }

    //add new student page
    function st_add_new_student_page()
    {
        require_once ST_VIEW_PATH . 'add.php';
    }

    //update student data page
    function st_update_data_page()
    {
        require_once ST_VIEW_PATH . 'edit.php';
    }

    //Loading Script & Css
    function st_load_script()
    {
        wp_enqueue_script('bootstrap-js','//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js',array('jquery-cdn'),true);
        wp_enqueue_style('bootstrap-css','//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css',true);
        wp_enqueue_style('style-css',ST_PLUGIN_URL.('css/style.css'),true);
        wp_enqueue_script('validate-js',ST_PLUGIN_URL.('js/validate_data.js'),array('jquery'),'1.0',false);

    }


    // Creating New Database
    function st_plugin_setup()
    {
            global $wpdb;
            $table_name = $wpdb->prefix . 'student_management';
            $charset_collate = $wpdb->get_charset_collate();

            $sql = "CREATE TABLE $table_name (
            id mediumint(9) NOT NULL AUTO_INCREMENT,
            name varchar(100) NOT NULL,
            email varchar(100) NOT NULL,
            number int(10) NOT NULL,
            address text NOT NULL,
            PRIMARY KEY  (id)
        ) $charset_collate;";

            require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
            dbDelta($sql);
    }
}

js代码:

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

});
function st_validate_add_data(){

    var name = jQuery('#name').val();
    var email=jQuery('#email').val();
    var number=jQuery('#pnumber').val();
    var address=jQuery('#address').val();
    var path=jQuery('#st-path').val();



    var flag=0;

    if(name=='')
    {
        jQuery('#name_error').html("Please enter student name");
        flag=1;
    }

    if(email=='')
    {
        jQuery('#email_error').html("Please enter student email");
        flag=1;
    }
    if(number=='')
    {
        jQuery('#number_error').html("Please enter student phone number");
        flag=1;
    }
    if(address=='')
    {
        jQuery('#address_error').html("Please enter student address");
        flag=1;
    }

    var data = {
        'action': 'my_action',
        'whatever': 1234
    };

    if(flag==0)
    {
        jQuery.ajax({
            type: 'POST',
            url: ajaxurl,
            dataType: 'json',
            data:'action=st_ajax_call',
            success:function(msg){
                alert(msg);
            },
            error: function(errorThrown) {
                alert(errorThrown);
            }
        });



    }
    return false;

}

我的数据库进程类(我想调用 ajax):

if(!class_exists('st_db_process')) {
    class st_db_process
    {
        function __construct()
        {
//        check_ajax_referer();
            add_action('wp_ajax_st_ajax_call', array($this, 'st_ajax_func'));
            add_action('wp_ajax_nopriv_st_ajax_call', array($this, 'st_ajax_func'));
        }

        function st_ajax_func()
        {
            echo "<pre>";
            print_r("helloo");
            echo "</pre>";
            echo "<pre>";
            print_r($_POST);
            echo "</pre>";
            exit();
            wp_die();
        }

        function st_add_data11($table_name, $data)
        {
            global $wpdb, $table_name;
            $wpdb->insert($table_name, $data);
        }

        function st_update_data($table_name, $data, $id)
        {
            global $wpdb, $table_name;
            $wpdb->update($table_name, $data, $id);
        }

        function st_get_data($query, $type)
        {
            global $wpdb;
            $data = $wpdb->get_results($query, $type);
            return $data;
        }
    }
    global $st_db_process;
    $st_db_process = new st_db_process();
}

请帮忙,我前两天一直在做这个任务。 我可以在我的主插件文件中调用 ajax,但无法在我想要的类文件中调用 ajax。

【问题讨论】:

  • 请检查您的控制台ajaxurl是否失败?
  • 是的兄弟它的工作,但每次响应得到 0

标签: php wordpress


【解决方案1】:

我得到了我的答案, st_db_process.php 文件未正确加载,我只需将其放在主插件文件的顶部即可。 这是文件延迟加载问题,我的代码很完美。 这一行放在插件开始,插件工作。

require_once ST_CLASS_PATH.'st_db_process.php';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    相关资源
    最近更新 更多