title: Failed to get the values of $_POST super global array inside a function in WordPress plugin creation
body: <p>I am new to wordpress and i am trying to create a simple plugin that inserts and fetches data into mysql database. The problem comes when i want to access the super global <code>$_POST</code> array  inside my <code>PHP function</code>. I can see the values being passed through <code>$_POST</code> array if i use <code>print_r($_POST)</code> function outside my <code>PHP function</code>. But if i try to use <code>print_r($_POST)</code> inside my function, it displays nothing. </p>
  
  <p>I am using <code>jQuery Post AJAX</code> request to submit the form. </p>
  
  <p>I want to access <code>$_POST</code> array inside my <code>PHP function</code> query mysql to fetch data from the database and return the results from the function where required.  </p>
  
  <p>My Plugin's directory structure looks like this.</p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>auc_result_fetcher.php contains this code</strong></p>
  
  <pre><code>/*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");
  </code></pre>
  
  <p><strong>search.php contains this code</strong></p>
  
  <pre><code>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(){
  ?&gt;
  &lt;div class="auc-search-form"&gt;
      &lt;div id="error"&gt;
  
          &lt;div id="select-class-error"&gt;&lt;/div&gt;
          &lt;div id="select-search-by-error"&gt;&lt;/div&gt;
          &lt;div id="search-field-error"&gt;&lt;/div&gt;
  
      &lt;/div&gt;
      &lt;form id="searchForm" method="get" action=""&gt;
          &lt;table&gt;
              &lt;tr&gt;
                  &lt;td style="text-align:right;"&gt; &lt;span&gt;Class:&lt;/span&gt; &lt;/td&gt;
                  &lt;td&gt;
                      &lt;select id="select-class"&gt;
                          &lt;option&gt;Select Class&lt;/option&gt;
                          &lt;?php
  
                          $grades = fetch_grades();
                          foreach($grades AS $v){
                              echo "&lt;option&gt;".$v-&gt;grade_title."&lt;/option&gt;";
                          }
                          ?&gt;
                      &lt;/select&gt;
                  &lt;/td&gt;
                  &lt;td id="class-select"&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr id="previous-row"&gt;
                  &lt;td style="text-align:right;"&gt; &lt;span&gt;Search By:&lt;/span&gt; &lt;/td&gt;
                  &lt;td&gt;
                      &lt;select id="search-by"&gt;
                          &lt;option&gt;Select Choice&lt;/option&gt;
                          &lt;?php
  
                          $grades = fetch_search_options();
                          foreach($grades AS $v){
                              echo "&lt;option&gt;".$v-&gt;search_title."&lt;/option&gt;";
                          }
                          ?&gt;
                      &lt;/select&gt;
                  &lt;/td&gt;
  
              &lt;/tr&gt;
              &lt;tr id="search-row"&gt;
                  &lt;td id="search-by-option-label" style="text-align:right;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/td&gt;
                  &lt;td id="search-by-field"&gt;&lt;input type="text" name="searchBy" id="search" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr &gt;
                  &lt;td &gt;&lt;/td&gt;
                  &lt;td&gt;
                      &lt;input type="hidden" id="url" name="url" value="&lt;?php echo SEARCH_RESULTS; ?&gt;"&gt;
                      &lt;input type="button"  id="search-button" value="Search" /&gt;
                      &lt;input type="reset" name="searchBy" id="reset" value="Cancel"/&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
      &lt;/form&gt;
      &lt;table id="results-table"&gt;
          &lt;tr&gt;
              &lt;th&gt;Roll Number&lt;/th&gt;
              &lt;th&gt;Name&lt;/th&gt;
              &lt;th&gt;School&lt;/th&gt;
              &lt;th&gt;Class&lt;/th&gt;
          &lt;/tr&gt;
          &lt;?php
          fetch_student_results();
          /*echo "&lt;pre&gt;";
          print_r($d);
          echo "&lt;/pre&gt;";*/    
          ?&gt;
      &lt;/table&gt;
  &lt;/div&gt;
  &lt;?php
  }
  </code></pre>
  
  <p><strong>fetch.php contains this code</strong></p>
  
  <pre><code>&lt;?php 
  
  // fetches student's grades for example: 9th, 10th grade
  function fetch_grades(){
  global $wpdb;
  $result = $wpdb-&gt;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-&gt;get_results("SELECT * FROM `wp-grade_search_options`");
  if($result){
       return $result;
  }
  return false;
  }
  
  //
  function fetch_student_results(){
    if(!empty($_POST))
     print_r($_POST);
  }
  </code></pre>
  
  <p><strong>auc-custom-jquery.js contains this code</strong></p>
  
  <pre><code>$(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);
  
     });
  
  });
  </code></pre>
  
  <p>EDIT: Please have a look on my detailed code so that you can understand what is going on. </p>
  
  <p>Please help me out how can i achieve this. This behavior seems wordpress specific. Please help me with this. Thanks in advance. </p>