Jquery ajax to custom php file: returning blank data

You are going on the wrong way.This will create difficulties and is NOT WORDPRESS STANDARD.Wordpress has a ajax technique to implement this.

Call the ajax from your page

<script>
var data = {
    'action': 'insert_data_customtable',
    'first_name': firstname,
    'last_name': lastname
};

// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php                 
jQuery.ajax({
  type: "POST",
  url: ajaxurl,
  dataType: "json",
  data: data,
  success: function(response) {
    console.log(response);
    ///response will return that you echo in function insert_data_customtable

  },
  error: function(response){
    alert(response);
  }
});

</script>

And write this in your functions.php

add_action("wp_ajax_nopriv_insert_data_customtable", "insert_data_customtable");
dd_action("wp_ajax_insert_data_customtable", "insert_data_customtable");

function insert_data_customtable(){
    //Here is your code for insert into table
    $insertid=mysql_insert_id();

    echo json_encode($insertid);
    die();
}

Leave a Comment