如何通过参数类的PHP与jQuery阿贾克斯 [英] How to pass parameter to class php with jQuery .ajax

查看:145
本文介绍了如何通过参数类的PHP与jQuery阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我隔壁班的PHP:

class Job{

    public $resultado;
    public $busqueda;
    public $wpdb;

    public function __construct($busqueda){
        global $wpdb;
        $this->busqueda = $busqueda;
        $this->resultado = array();
        $this->wpdb = $wpdb;
    }   

    public function search_job($resultado){
        $tablepost =$this->wpdb->prefix.'posts';
        $query = "SELECT * from $tablepost WHERE post_type = 'jobman_job' and post_status='publish';";



        if (isset($wpdb)) {
            global $wpdb;
            $result = $wpdb->get_results($query);
        }else{
            $result = $this->wpdb->get_results($query);
        }



        print_r($result);
    }

}

此找到确定。现在,我想打电话与jQuery的search_function阿贾克斯 我尝试用这样的:

This found ok. Now I would like call the search_function with jQuery .ajax I try with this:

(function($){

    $('#searchjob').keypress(function(e){
        var search = $.trim($('#searchjob').val());

        if (e.which == "13") {

            $.ajax({
                beforeSend: function(){
                    //$('#loading').html(<img src="rutagif" alt="loading" />);
                },
                url:"../wp-content/themes/SUP2012/class/job.php",
                data:{method: 'search_job', data:{resultado: 'hola'}},
                type: "POST",
                success: function(data){

                }

            }); 
        };

    });

 })(jQuery);

网址参数的控制OK(200 OK),但不retrive信息。你知道吗?

the url parameters respond ok (200 OK), but not retrive information. Any idea?

推荐答案

为使字preSS的 AJAX 要求你应该在的functions.php

To make an ajax request in wordpress you should use in functions.php

add_action('wp_ajax_nopriv_your_function_name', 'your_function_name');
add_action('wp_ajax_your_function_name', 'your_function_name');
function your_function_name()
{
    // do anything here and echo the result
    $data_from_ajax=$_POST['data_to_send_to_server'];
    die(); // last line 
}

和JavaScript的形式应为

And the javascript should look like

$('#searchjob').keypress(function(e){
    var your_data = $.trim($(this).val());
    if (e.which == "13") {
        $.ajax({
            type:"POST",
            url: "./wp-admin/admin-ajax.php", // if this file is in a subfolder in your themes folder
            data: {
                action:'your_function_name', // the function name you used in functions.php
                data_to_send_to_server:your_data // retrieve from $_POST in php
            },
            success:function(data){
                // do something with data
            }
        });
    }
});

这篇关于如何通过参数类的PHP与jQuery阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆