繁体   English   中英

如何使用 Ajax 在 Wordpress 插件中调用外部 php 文件

[英]How to call an external php file inside a Wordpress plugin using Ajax

我是 Wordpress 插件的新手,所以在这种情况下,我需要使用位于插件文件夹内的 includes 文件夹中的名为 action_save_data.php 的文件。

所以插件代码如下所示:

<?php
  /**
  * Plugin Name: ETBO test
  * Plugin URI:  http://cma-ecg.com/ebot/
  * Text Domain: ETBO-TEST
  * License:     GPL-2.0+
  * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
  */

 add_shortcode('etbo_test' , 'render_test');

 function render_test(){ 


 ?>
  <html lang="en" >
     // All the Html Code here...
 

   <script src="wp-content/plugins/etbo/assets/js/wizard-3.js"></script>

</html>
 <?php 
  }
  ?>

在 wizard-3.js 文件中我有这段代码:

function saveData(){

var data = $("#kt_form").serialize();
         
        
          
           $.ajax({
            
                       type : 'POST',
                       url  : 'includes/action_save_data.php',
                       data: data, 
                       success :  function(response)
                          {      
                             if(response==1){
                                 
                                 Swal.fire({
                                        text: "Resultados Guardados y Enviados por Correo.",
                                        icon: "success",
                                        buttonsStyling: false,
                                        confirmButtonText: "Ok, entendido!",
                                        customClass: {
                                            confirmButton: "btn font-weight-bold btn-primary",
                                        }
                                    });
                                 
                             }else{
                                 
                                 alert(response);
                             }
                          }
                          
                });

              }

控制台日志显示找不到 action_save_data.php 文件,我知道该文件存在于该文件夹中,所以如何包含它? 提前致谢

您可以在加载 js 文件之前声明一个 Javascript 变量var ajax_target = "<?php echo plugins_url( './includes/action_save_data.php', __FILE__ )?>" ,然后在 JS 文件的 AJAX 代码中使用此变量如下。 `$.ajax({

                   type : 'POST',
                   url  : ajax_target,

.........`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM