Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. <script type="text/javascript">
  2. function doAjax(){
  3. var data1 = jQuery("#data1").val();
  4. var data2 = jQuery("#data2").val();
  5.  
  6. var myajaxurl = '<?php echo admin_url('admin-ajax.php') ?>';
  7. var data = new FormData();
  8.  
  9. //parametro action รจ richiesto da wp-ajax รจ l'hook usato per il match della funzione (wp_ajax_itc_sample_ajax o wp_ajax_nopriv_itc_sample_ajax)
  10. data.append('action','itc_sample_ajax');
  11. data.append('method', 'delet');
  12. data.append('data1', data1);
  13. data.append('data2', data2);
  14.  
  15.  
  16. jQuery.ajax({
  17. url: myajaxurl,
  18. data: data,
  19. cache: false,
  20. contentType: false,
  21. processData: false,
  22. type: 'POST',
  23. error: function(json){
  24. console.log('error');
  25. },
  26. success: function(json){
  27. console.log(json);
  28. var data = jQuery.parseJSON( json );
  29. console.log(data);
  30. }
  31. });
  32.  
  33.  
  34.  
  35. </script>
  36.  
  37.  
  38. <?php
  39. class ItcAjaxSample
  40. {
  41.  
  42.  
  43.  
  44. /**
  45. *
  46. * @var ItcAjaxSample
  47. */
  48. private static $instance = null;
  49.  
  50.  
  51. /**
  52. *
  53. * @return ItcAjaxSample
  54. */
  55. public static function get_instance() {
  56. if ( null == self::$instance ) {
  57. self::$instance = new self;
  58. }
  59. return self::$instance;
  60.  
  61. }
  62.  
  63. protected function __construct() {
  64.  
  65. add_shortcode( 'itc_ajax_sample',array($this,'do_ajax_sample'), 10, 2);
  66. add_action( 'wp_enqueue_scripts', array($this,'enqueue_script') );
  67.  
  68. //solo se anche pubblico (no wp-admin)
  69. add_action( 'wp_ajax_nopriv_itc_sample_ajax', array($this,'itc_sample_ajax') );
  70. //per wp-admin
  71. add_action( 'wp_ajax_itc_sample_ajax', array($this,'itc_sample_ajax') );
  72.  
  73. }
  74.  
  75. public function do_ajax_sample($atts, $content = null ) {
  76.  
  77. $a = shortcode_atts( array(
  78. 'attr1' => null,
  79. ), $atts );
  80.  
  81. $attr1 = $a["attr1"];
  82.  
  83. $path = plugin_dir_path(__FILE__) . 'templates/';
  84.  
  85. //composer "league/plates": "^3.3"
  86. $templates = new League\Plates\Engine( $path , "tpl");
  87.  
  88.  
  89. $data = [];
  90. $data['title'] = "PROVA";
  91. $data['ajaxurl'] = admin_url('admin-ajax.php');
  92.  
  93. $html = $templates->render('itc-pbx-innovaphone-reporting', $data);
  94.  
  95. echo $html;
  96.  
  97. }
  98.  
  99. public function enqueue_script() {
  100.  
  101. wp_enqueue_script( 'itc_sample1', plugins_url() . '/myplugin/js/sample.js', array( 'jquery' ), '1.1.1', false );
  102. wp_enqueue_script( 'itc_sample2', plugins_url() . '/myplugin/js/sample.js', array( 'jquery' ), '1.2.2', false );
  103.  
  104.  
  105. }
  106.  
  107.  
  108. public function itc_sample_ajax(){
  109. echo json_encode($_POST);
  110. die();
  111. }
  112.  
  113.  
  114.  
  115. }
  116.  
  117.  
  118. $ItcPbxInnovaphoneReporting = ItcPbxInnovaphoneReporting::get_instance();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement