How to pass parameter to class php with jQuery .ajax

I have the next class 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);
    }

}

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

Read More
(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);

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

Related posts

Leave a Reply

1 comment

  1. 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 
    }
    

    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
                }
            });
        }
    });