2013-04-23 81 views
0

使用AJAX將PHP POST到自身不會返回預期值嗎?使用AJAX將PHP POST到自身不會返回期望值?

參數已成功發送到POST,基於Firebugs Console ,但它不顯示結果到同一頁面。

如何解決這個問題?

labor.php

<?php 

if(isset($_POST['from']) && isset($_POST['from'])) 
{ 
    echo 'from: '.$_POST['from']. 
    ' to: '.$_POST['to']; 
    //do something here 
} 

?> 

<!doctype html> 

<html lang="en"> 
<head> 
    <meta charset="utf-8" /> 
    <title>Labor Reports</title> 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" /> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
    <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script> 
    <link rel="stylesheet" href="/resources/demos/style.css" /> 
    <script> 
    $(function() { 
    $("#datepicker_from").datepicker(); 
    $("#datepicker_to").datepicker(); 
    $("button") 
     .button() 
     .click(function() { 
     $.ajax({ 
      url: 'labor.php', 
      type: 'POST', 
      data: { from: $('#datepicker_from').val().trim(), to: $('#datepicker_to').val().trim() }, 
      success: function(){ 

      } 
     }); 
     }); 
    }); 

回答

2

你是不是做與返回的數據什麼。

success: function(data){ 
    alert(data); 
} 

data表示從服務器發回的響應。在你的情況下,它將是你的fromto $ _POST值的回顯輸出。巧合的是,你也會將頁面的其餘部分發回給你,你可能會想要在回聲後return false;,以便它停止頁面的打印。