php - 无法在 WordPress 插件创建中的函数内获取 $_POST super 全局数组的值

标签 php mysql wordpress post

我是 WordPress 新手,我正在尝试创建一个简单的插件,用于将数据插入和提取到 mysql 数据库中。当我想访问 PHP 函数 内的 super 全局 $_POST 数组时,问题就出现了。如果我在 PHP 函数 之外使用 print_r($_POST) 函数,我可以看到通过 $_POST 数组传递的值。但是,如果我尝试在函数中使用 print_r($_POST) ,它不会显示任何内容。

我正在使用 jQuery Post AJAX 请求来提交表单。

我想访问 PHP 函数 查询 mysql 中的 $_POST 数组,以从数据库获取数据,并在需要时从函数返回结果。

我的插件的目录结构如下所示。

auc_result_fetcher //this is the main plugin folder

    auc_result_fetcher.php // This file includes other scripts using include function
    includes // directory containing other files to be included

         fetch.php   // this file contains the functions to fetch data from mysql db 
         scripts.php // this file contains the enqueue code of js and css scripts  
         search.php  // this file contains form to submit data and also displays it 

         js //directory contsins js files

            auc-custom-jquery.js 

         css //directory contsins css files

            auc-custom-stylesheet.css

auc_result_fetcher.php 包含此代码

/*plugin header information goes here..*/

/************
AUC Constants
*************/

define("PAGE_URI", plugins_url());
define("SEARCH_RESULTS", PAGE_URI."/auc_result_fetcher/includes/fetch.php/");

/************
AUC Includes
*************/
include ("includes/scripts.php");
include ("includes/fetch.php");
include ("includes/search.php");

search.php 包含此代码

add_action('init', function() {
   add_shortcode('search_form', 'print_search_form');
});
add_action('init', 'fetch_grades');
add_action('init', 'fetch_search_options');
add_action('init', 'fetch_student_results');


function print_search_form(){
?>
<div class="auc-search-form">
    <div id="error">

        <div id="select-class-error"></div>
        <div id="select-search-by-error"></div>
        <div id="search-field-error"></div>

    </div>
    <form id="searchForm" method="get" action="">
        <table>
            <tr>
                <td style="text-align:right;"> <span>Class:</span> </td>
                <td>
                    <select id="select-class">
                        <option>Select Class</option>
                        <?php

                        $grades = fetch_grades();
                        foreach($grades AS $v){
                            echo "<option>".$v->grade_title."</option>";
                        }
                        ?>
                    </select>
                </td>
                <td id="class-select"></td>
            </tr>
            <tr id="previous-row">
                <td style="text-align:right;"> <span>Search By:</span> </td>
                <td>
                    <select id="search-by">
                        <option>Select Choice</option>
                        <?php

                        $grades = fetch_search_options();
                        foreach($grades AS $v){
                            echo "<option>".$v->search_title."</option>";
                        }
                        ?>
                    </select>
                </td>

            </tr>
            <tr id="search-row">
                <td id="search-by-option-label" style="text-align:right;"><span></span></td>
                <td id="search-by-field"><input type="text" name="searchBy" id="search" /></td>
            </tr>
            <tr >
                <td ></td>
                <td>
                    <input type="hidden" id="url" name="url" value="<?php echo SEARCH_RESULTS; ?>">
                    <input type="button"  id="search-button" value="Search" />
                    <input type="reset" name="searchBy" id="reset" value="Cancel"/>
                </td>
            </tr>
        </table>
    </form>
    <table id="results-table">
        <tr>
            <th>Roll Number</th>
            <th>Name</th>
            <th>School</th>
            <th>Class</th>
        </tr>
        <?php
        fetch_student_results();
        /*echo "<pre>";
        print_r($d);
        echo "</pre>";*/    
        ?>
    </table>
</div>
<?php
}

fetch.php 包含此代码

<?php 

// fetches student's grades for example: 9th, 10th grade
function fetch_grades(){
global $wpdb;
$result = $wpdb->get_results("SELECT * FROM `wp-student-grade`");
if($result){
     return $result;
}
return false;
}

// fetches student's grades search options for example: by name, by roll number, by school
function fetch_search_options(){
global $wpdb;
$result = $wpdb->get_results("SELECT * FROM `wp-grade_search_options`");
if($result){
     return $result;
}
return false;
}

//
function fetch_student_results(){
  if(!empty($_POST))
   print_r($_POST);
}

auc-custom-jquery.js 包含此代码

$(document).ready(function(){

       var selectCls = $("select#select-class").val();
       var searchBy = $("select#search-by").val();
       var searchField = $("input#search").val();
   var url = $("input#url").val(); 

       $.post(url, {c: selectCls, s: searchBy, f: searchField}, function(data){

    alert(data);

   });

});

编辑:请查看我的详细代码,以便您了解发生了什么。

请帮助我如何实现这一目标。这种行为似乎是 WordPress 特有的。请在这件事上给予我帮助。提前致谢。

最佳答案

抱歉,问题读得有点快。

好的,您正在将表单提交给例如myfile.php

myfile.php 中,您可以执行 print_r($_POST) 操作。如果是,那么:

$data = $_POST;

$result = do_something($data);


function do_something($data){
  // Do something with the $data and return result
}

关于php - 无法在 WordPress 插件创建中的函数内获取 $_POST super 全局数组的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22145929/

相关文章:

php - 从 WooCommerce 中的面包屑中删除 "Shop"

php - Ajax 无法与 wpdb 一起使用

php - 使用 PHP 从 XML 表中提取 NHL 排名

javascript - 将日期函数从 javascript 转换为 php

php - MySQL 返回一个空结果集

MySQL 通过比较日期选择事件、过期和不活动三个结果

javascript - 在 php 函数内部时,Sql 查询不起作用

wordpress - 如何使用 Docker 容器运行多个独立的 Wordpress 网站

php - MySQL数据库实时更新网页

php - Symfony2 Doctrine,通过外键连接表