1

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:

(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?

13
  • I checked the information through firebug but the response is blank Commented Oct 20, 2012 at 4:50
  • 1
    you have just call the php file but you want have specified how your class will initialize and method will be called Commented Oct 20, 2012 at 4:55
  • How did you initialize the class or you didn't ? Commented Oct 20, 2012 at 4:56
  • what is beforeSend and why have you defined this inside ajax call Commented Oct 20, 2012 at 4:58
  • 1
    @refhat beforeSend(jqXHR, settings)Function A pre-request callback function that can be used to modify the jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object before it is sent. Use this to set custom headers, etc. The jqXHR and settings maps are passed as arguments. This is an Ajax Event. Returning false in the beforeSend function will cancel the request. As of jQuery 1.5, the beforeSend option will be called regardless of the type of request. Commented Oct 20, 2012 at 5:03

1 Answer 1

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
            }
        });
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

I have just try out the code and work very good!. I'm working with wordpress and a custom template on wordpress! :D.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.