Scroll to top

How to fetch data from database in cordova app with ajax in wordpress?

fetch data from database in cordova app with ajax in wordpress.


Paste in funtion.php file.

 function fetch_cordova_message(){

if(function_exists(‘date_default_timezone_set’)) {
date_default_timezone_set(“Asia/Kolkata”);
}

// then use the date functions, not the other way around
$date = date(“d/m/Y”);
$date1 =  date(“H:i a”);

global $wpdb;
$table_name = $wpdb->prefix .”customer”;
$result = $wpdb->get_results ( “SELECT * FROM $table_name Order by ID desc” );
foreach ( $result as $print ) 
{
            echo'<div class=”list_row”><div class=”per_name”>’.$print->person .'</div><div class=”full_view”>></div></div>’;
        }

die();
 }

 add_action(‘wp_ajax_nopriv_fetch_cordova_message’,’fetch_cordova_message’);
 add_action(‘wp_ajax_fetch_cordova_message’,’fetch_cordova_message’);

function add_cors_http_header(){
    header(“Access-Control-Allow-Origin: *”);
}
add_action(‘init’,’add_cors_http_header’);

add_filter( ‘wp_headers’, ‘send_cors_headers’, 11, 1 );
function send_cors_headers( $headers ) {
    $headers[‘Access-Control-Allow-Origin’] = $_SERVER[ ‘HTTP_ORIGIN’ ];
    return $headers;
}

Paste in main file.

jQuery(document).ready(function($){
function fetch_customer_list(){
var ajax = “https://wpcodekit.com/wp-admin/admin-ajax.php”;
  var data = {
‘action’ : “fetch_cordova_message”
}
$.post(ajax,data,function(response){
//$(“#response”).html(response);
//console.log(response);
$(‘.customer_list’).html(response);
});
}
fetch_customer_list();
});

Leave a Reply

Your email address will not be published. Required fields are marked *