AJAX函数将post放在URL中,而不是页面

时间:2018-01-07 23:17:50

标签: php ajax post

我有一个下拉列表,选中后,应该查询数据库并获取报告。当用户从下拉列表中选择该选项时,它应该动态更改字段而不重新加载页面。

但是,它确实会重新加载页面,并且会在URL中插入并显示$ _POST数据。

我的自定义页面中的代码(这是在Wordpress btw,但我不认为这是一个Wordpress问题)

<form class="get_monthly_report_form" role="form" action="">
<input type="submit" id="function" name="function" value="Retrieve Whole Report">  {code excised here that fills the dropdown}

</form>

<div id="search_results"></div>

这是来自.js文件的代码

//Monthly Reports
<script>  
// wrap everything in a closure
(function($){

// get our references
var $form = $('form.get_monthly_report_form'),
  $search_field = $('#function'),
  $results = $('#search_results');

// AJAX search call 
function do_search_reps() {

// grab the query value from the search field
var search_text = $search_field.val();

// do a POST ajax call
$.ajax({
  type: "POST",
  url: '<?php echo admin_url('admin-ajax.php'); ?>',
  data: ({
    action: "get_and_view_report",
    search_text: search_text
  }).serialize(),
  success: function (response){
    console.log(response);
    $results.html(response);
  }
}); }
 // on submit, do the search but return false to stop page refresh   
$form.submit(function(e) {
e.preventDefault();
do_search_reps();
return false;
 });

})(jQuery);

我的project_functions文件中的代码是

function get_and_view_report()
{ var_dump ($_POST);

// first, get data passed
$data_passed = explode("++",$_POST["monthly_report_ID"]);

$report_key = $data_passed[0];
$report_type = $data_passed[1];

                //print_r ($report_key);
                //print_r ($report_type);
//get data about the report as a whole.
$sql = "SELECT * FROM reports_list WHERE report_key ='" . $report_key . "'";
$report_data = $wpdb->get_results ($sql);

//if statement to set up which table to query from NEED TO FINISH THIS
if (substr ($report_data[0]->report_key,0,3) == "sdr")
{
    $sql = "SELECT * FROM deployment_list_rows WHERE report_key = '" . $report_key . "' ORDER BY line_number ASC";
}
elseif (substr ($report_data[0]->report_key,0,3) == "Off")
{
    $sql = "SELECT * FROM officer_list_rows WHERE report_key = '" . $report_key . "' ORDER BY line_number ASC";
}   
elseif (substr ($report_data[0]->report_key,0,4) == "stat")
{
    $sql = "SELECT * FROM status_report_rows WHERE report_key = '" . $report_key . "' ORDER BY line_number ASC";
}
elseif (substr ($report_data[0]->report_key,0,3) == "wtr")
{
    $sql = "SELECT * FROM service_time_wages_lines WHERE report_key = '" . $report_key . "' ORDER BY line_number ASC";
}
elseif (substr ($report_data[0]->report_key,0,3) == "flr")
{
    $sql = "SELECT * FROM fleet_ships_list_rows WHERE report_key = '" . $report_key . "' ORDER BY line_number ASC";
}

//print_r ($sql);
$reports_in_db = $wpdb->get_results($sql);

if (empty($reports_in_db))
{
    echo "No Data Returned. Something's Wrong";
}
else
{
    //print_r ($reports_in_db);     
    //print_r ($report_data);

    //Time to start building the file to go out.
    //so we're going to build an array of things, bringing together all the things 

    $total_report = array();
    foreach($reports_in_db as $key)
    {
        $total_report [$key->line_number] = $key;                       
    }

    // get subtitles/abstract
    $sql = "SELECT line_number, field_text FROM subtitle_abstract_summary WHERE report_key = '" . $report_key . "'";            
    $abs_sums = $wpdb->get_results ($sql);
    //now for a series of conditional things
    foreach ($abs_sums as $key)
    {
        $total_report [$key->line_number] = $key;
    }

    ksort ($total_report, SORT_NUMERIC); //sorting the lines and subtitles into the correct order.

    //Now an if statment- calling the sort functions based on report type- NEEDS TO BE DONE
    $arrs_to_be_printed =   sort_ship_deployment_data_single_report ($total_report);                        

    //Now Create the File headers
    $file_headers = array($report_data[0]->report_title, $report_data[0]->report_date, $report_data[0]->transcription_notes, $report_data[0]->labels_row, $arrs_to_be_printed[0]);

    print_report_to_screen ($file_headers, $arrs_to_be_printed[1]);
}
}
add_action('wp_ajax_get_and_view_report', 'get_and_view_report');
add_action('wp_ajax_nopriv_get_and_view_report', 'get_and_view_report');

当我点击提交(并尝试使用Ajax调用 - 会发生什么事情是页面重新加载(当我不想要它时),现在URL就像 “视图月度报告/ monthly_report_ID = OffRec1514581778%2B%2Bofficer_list&安培;功能=检索+全体+报告”

在我看来,将帖子数据放入网址 - 而在函数var_dump中,结果是

"array(0) { } No Data Returned. Something's Wrong"

当我进行测试时,似乎动作挂钩无效。

我将不胜感激。

0 个答案:

没有答案