0

I've created a PHP script that works great stand alone.

I need to convert this script to a WordPress plugin. I've created a custom table in the WordPress database.

When i try to get data back I get the wp-admin main page. All the data needs shown on the wp-admin page.

function applyfilter() {
    var timeperiod  = $( '#timefilter' ).val();
    var datefrom    = $( '#pdate1' ).val();
    var dateto      = $( '#pdate2' ).val();
    var status      = $( '#status' ).val();

    $('#lists').html("Loading....");
    $.ajax( {
        url     : 'index.php',
        type    : 'POST',
        data    : 'action=ajax&timeperiod=' + timeperiod + '&datefrom=' + datefrom + '&dateto=' + dateto + '&status=' + status,
        success : function( response ) {
            $( '#lists' ).html( response );
        }
    } );
}

index.php is the main file in my plugin folder.

1

You can use like this for send ajax...

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

            var data = {
                'action': 'my_action',
                'whatever': '1'
            };

            $.post(ajaxurl, data, function (response) {
                if ($('#menu_order').val() == '' || $('#menu_order').val() == '0')
                    $('#menu_order').val(response);
            });
        });
    </script> <?php
}

add_action('admin_footer', 'my_action_javascript'); 

function my_action_callback()
{
    $whatever = intval($_POST['whatever']);
    //$whatever += 1;
    echo $whatever;
    wp_die(); // this is required to terminate immediately and return a proper response
}
add_action('wp_ajax_my_action', 'my_action_callback');
| improve this answer | |
0

Please refer this tutorial which detail steps for how to use ajax. may help you http://www.makeuseof.com/tag/tutorial-ajax-wordpress/

| improve this answer | |
  • 1
    Link only answers are not quite recommended here. You can add some explanation along with link. – Nilambar Sharma Dec 26 '14 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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