如何将AA JS变量传递给PHP?

时间:2020-09-21 作者:stemon

我正在使用JS从API获取数据。如何在PHP中使用这些数据。更具体地说,将此数据分配给$_SESSION PHP中的变量,以便我可以在其他页面(模板文件)中使用它?

2 个回复
SO网友:Suresh Shinde

步骤1:运行wp\\u localize\\u脚本,在函数中添加以下函数。php


 add_action( \'wp_enqueue_scripts\', \'jsto_php_enqueue_scripts\', 5 );
 public function jsto_php_enqueue_scripts() {
          wp_register_script( \'file_handle\', plugin_dir_url( __FILE__ ) . \'js/js-file-name.js\', array( \'jquery\' ), \'0.0.1\', false );  
          wp_enqueue_script(\'file_handle\');
          // Set Nonce Values so that the ajax calls are secure
          $add_something_nonce = wp_create_nonce( "add_something" );
          $ajax_url = admin_url( \'admin-ajax.php\' );
          $user_id = 20; //PHP variable
          // pass ajax object to this javascript file
          // Add nonce values to this object so that we can access them in the plugin-name-admin.js javascript file
          wp_localize_script( \'file_handle\', \'ajax_object\', 
            array( 
                \'ajax_url\' => $ajax_url,
                \'add_something_nonce\'=> $add_something_nonce,
                \'user_id\' => $user_id
            ) 
          );


 }


步骤2:访问JavaScript文件中的本地化变量(在我们的示例js File name.js中)



(function( $ ) {

    \'use strict\';
     console.log(ajax_object);
     console.log(ajax_object.ajax_url);
     console.log(ajax_object.user_id);
     console.log(ajax_object.add_something_nonce);

})( jQuery );

SO网友:Pirate

在过去的几天里,我一直在努力解决同样的问题。

我是Wordpress的新手,所以这可能不是一个百分之百正确的方法。如果有人发现任何错误,我将尝试更正(如果发现任何错误,请发表评论)。

以下是最终对我起作用的代码:

插件的主php文件:

<?php
/*
Plugin Name: My plugin
Description:
Version: 1.0
Author: Pirate
*/
class MyPlugin
{
    public function __construct()
    {
        // Adding an action for both logged and not logged users
        add_action(\'wp_ajax_myplugin_action\', array($this, \'myplugin_handle_ajax\'));
        add_action(\'wp_ajax_nopriv_myplugin_action\', array($this, \'myplugin_handle_ajax\'));

        // Adding action to wp_enqueue_scripts allows you to create nonces in the callback function
        add_action(\'wp_enqueue_scripts\', array($this, \'myplugin_load_scripts_styles\'));
    }

    public function myplugin_load_scripts()
    {
        wp_enqueue_script(\'myplugin_scripts\', plugins_url(\'myplugin_scripts.js\'), null, \'1.0\', true);

        $nonce = wp_create_nonce( \'ajax_validation\' );
        wp_localize_script(\'myplugin_scripts\', \'myplugin_data\',
            array(
                \'url\' => admin_url(\'admin-ajax.php\'),
                \'security\' =>  $nonce,
                \'additional_data\' = \'Anything else you need to send to JS\'
            )
        );
    }

    public function myplugin_handle_ajax()
    {
        check_ajax_referer( \'ajax_validation\', \'security\' );

        // All the data you sent from JS is in the $_REQUEST
        $dataFromJS = $_REQUEST[\'dataFromJS\'];
        
        // Here you can do whatever you want with it - process it, send it to other methods etc.

        $dataToSendBack = \'Data I might want to send back to JS\';
        wp_send_json_success($dataToSendBack);  //parameter is optional
    }

//Instantiate the class
$my_plugin = new MyPlugin();
myplugin\\u脚本。js公司

// I did not have to wrap this in $(document).ready(),
// but it might be necessary depending on the execution order of your particular script
$(\'#ajax_button\').on(\'click\', function () {
    let ajax_data = {
        action: \'myplugin_handle_ajax\',
        security: myplugin_data.secuirty,
        dataFromJS: \'Whatever I want to send to PHP\'
    };

    $.ajax({
        url: myplugin_data.url,
        method: \'POST\',
        data: ajax_data,
        success: function (response) {
            //optional handling of the response
        }
    })
});

相关推荐

使用PHP编写WordPress AJAX表单以检查用户是否已注销并显示错误

我有一个WordPress前端表单,它使用AJAX保存数据。表单是在后端处理的,显然是使用PHP post,我做了一个简单的解决方案,如果表单无法提交,就向用户通知表单提交错误。但我的表单是针对已登录用户的,如果WordPress用户已注销,我希望我的表单显示错误消息。<form action="https://milyin.com/my/" method="post" name="milyin-add-edit" id="Add