0

i am using custom plugin to display the values on the wordpress admin side..

My Jquery html file is

<script type='text/javascript'>
                    jQuery(document).ready(function(){
                    jQuery("#searchsubmit").click( function(e) {
                    e.preventDefault();
                    var data = jQuery("#eventname").val();
                    var city =  jQuery("#city").val();
                    var date =  jQuery("#datepicker").val();

                    jQuery.ajax({
                            type:'POST',
                            url: './admin-ajax.php',
                            data:{action:'my_action',test:data,test1:city,test2:date},
                            dataType: 'html',                                   
                            success: function(response) {
                                alert(response);
                            }
                           });
                            });

                    });
                </script>

My Plugin File is

add_action('wp_ajax_my_action', 'my_action_callback');

function my_action_callback(){

        $name = $_POST['test'];
        $city = $_POST['test1'];
        $date = $_POST['test2'];

        if ($date != '') {
            $enddate = date("Y-m-t", strtotime($date));
            $dates_sql = " AND start_date <= '$date' ";
            }
            if ($city != '' && $city != -1) 
            $destination_sql = "AND venue = '$city'";
            if ($name != '' && $name != -1) 
                $origincity_sql = "AND event_name = '$name'";

        $query = " SELECT * FROM <table name> WHERE event_id!='' " . $dates_sql . " " . $destination_sql . " " . $origincity_sql . " ORDER BY start_date DESC ";

        $results = $wpdb->get_results($query) or die(mysql_error());    

        foreach ($results as $row)
        {
            $message  = "<tr><td align='center' id = 'username'>".$row->username."</td><td align='center' id = 'eventname'>".$row->event_name."</td><td align='center' id = 'venue'>". $row->venue."</td><td align='center' id = 'email'>".$row->email."</td><td align='center' id = 'misseddays'>".$row->missed_days."</td><td align='center' id = 'paymentstatus'>".$row->payment_mode."</td><td align='center' id = 'amountpaid'>".$row->amount_paid."</td><td align='center' id = 'amountbalance'>".$row->amount_balance."</td><td align='center' id = 'mailsent'>".$row->mail."</td></tr>";
        }  

        echo $message;
        exit();
    }

i am not getting the response into the jquery html file despite it is returning -1 as response any changes to be done please help.stuck today on this one.

1 Answer 1

0

You are not using the Wordpress ajax API correctly, see Ajax in Plugins. Tha action parameter defines a function callback for wp_aja_* action hook, not the name of a file:

<?php
add_action( 'wp_ajax_my_action', 'my_action_callback' );
//Uncomment next line if you want the ajax action enable in the frontend
//add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' );
function my_action_callback() {
    global $wpdb;
    $name = $_POST['test'];
    $city = $_POST['test1'];
    $date = $_POST['test2'];

    if ($date != '') {
        $enddate = date("Y-m-t", strtotime($date));
        $dates_sql = " AND start_date <= '$date' ";
    }
   if ($city != '' && $city != -1) {
        $destination_sql = "AND venue = '$city'";
   }
   if ($name != '' && $name != -1) {
            $origincity_sql = "AND event_name = '$name'";
   }

   $query = " SELECT * FROM <table name> WHERE event_id!='' " . $dates_sql . " " . $destination_sql . " " . $origincity_sql . " ORDER BY start_date DESC ";

    $results = $wpdb->get_results($query) or die(mysql_error());    

    foreach ($results as $row) {
        $message  = "<tr><td align='center' id = 'username'>".$row->username."</td><td align='center' id = 'eventname'>".$row->event_name."</td><td align='center' id = 'venue'>". $row->venue."</td><td align='center' id = 'email'>".$row->email."</td><td align='center' id = 'misseddays'>".$row->missed_days."</td><td align='center' id = 'paymentstatus'>".$row->payment_mode."</td><td align='center' id = 'amountpaid'>".$row->amount_paid."</td><td align='center' id = 'amountbalance'>".$row->amount_balance."</td><td align='center' id = 'mailsent'>".=$row->mail."</td></tr>";
    }  
    header('Content-Type: application/json');
    echo json_encode($message);
    exit;
}

?>

You may also take a look to wp_send_json, wp_send_json_error and wp_send_json_success.

3
  • i defined the api in the plugin file.i am doing it on the backend.its returning to wordpress option page ..
    – chinna
    Commented Jul 8, 2014 at 13:40
  • I'm sorry but I don't understand what you mean with "I defined the API...". Can you expand? I think that you are putting the code I posted in the file my_action.php but you are not including the file anywhere, so the code in my_action.php is not executed. Try to include the code I posted in the main file of the plugin or in the functions.php file of your theme.
    – cybmeta
    Commented Jul 8, 2014 at 17:56
  • its worked i missed the functionality to be placed under theme functions.php file.Thanks @Cybmeta
    – chinna
    Commented Jul 9, 2014 at 5:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.