-1

I tried to write ajax with jquery to fetch data from database butajax doesn't return any data. I put that code in header and it return the whole html content in response. What is the code need to write in admin-ajax.php to use ajax?

1
0

Write below code in functions.php of your active theme

<script type="text/javascript" >
jQuery(document).ready(function($) {

    var data = {
        'action': 'get_data',
        'testparam':'hello'  //optional to pass any extra param
    };


    $.ajax(ajaxurl, data, function(response) {
        alert('Response: ' + response);
                //Append response in your result wrapper
    });
});
</script>

<?php 


add_action( 'wp_ajax_get_data', 'get_data_ajax_func' );

function get_data_ajax_func() {
    global $wpdb; 

    $param =  $_POST['testparam'] ;

        $html = "result"; // get data from database
        echo $html; //return result

    exit(); 
}
?>

Firebug the ajax request to check its working! Hope it will help!

2
  • Fatal error: Call to undefined function add_action() in function.php what does this mean? please help me
    – nishi
    Aug 26 '14 at 7:23
  • are u using localserver or any hosting for this
    – Balas
    Aug 26 '14 at 7:57

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