Scroll to top

How to insert data in database with ajax in cordova?

Insert data in database with ajax in cordova.

Paste in function.php file

 function insert_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”);

$person = $_POST[‘person’];
$email = $_POST[’email’];
$phone = $_POST[‘phone’];
$password = $_POST[‘pass’];

global $wpdb;
$table_name = $wpdb->prefix .”customer”;
$wpdb->insert($table_name, array(‘person’ => $person, ’email’ => $email,’phone’ => $phone, ‘password’ => $password,’registered_date’ => $date) );

  die();
 }

 add_action(‘wp_ajax_nopriv_insert_cordova_message’,’insert_cordova_message’);
 add_action(‘wp_ajax_insert_cordova_message’,’insert_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


$(‘.registration’).click(function(e) {
e.preventDefault();

var person = $(“.person”).val();
var email = $(“.email”).val();
var phone = $(“.phone”).val();
var pass = $(“.password”).val();
var ajax = “https://wpcodekit.com/wp-admin/admin-ajax.php”;

if(person==”){
alert(‘All field are required’);
return false;

}
if(email==”){
alert(‘All field are required’);
return false;

}
  if(phone==”){
alert(‘All field are required’);
return false;

}
  if(pass==”){
alert(‘All field are required’);
return false;

}

//alert(pass);

  var data = {
‘action’ : “insert_cordova_message”,
‘person’: person,
’email’ : email,
‘phone’ :phone,
‘pass’ : pass
}


$.post(ajax,data,function(response){

location.reload(true);
});

});

Leave a Reply

Your email address will not be published.