2016-01-17 38 views
1

誰能幫助我?

我有一個將參數(從數組中取出)帶入PHP $ _POST的問題。 $ ajax發送的參數已經填滿了。當我嘗試將整個數組抓到PHP腳本中時,沒有可用的$ _POST或$ _GET。我錯了什麼。我也試過它也是單參數,也失敗了。我嘗試了所有可能的方式來抓取參數($ _GET以及$ _POST)。我錯了什麼?

例如輸入已被定義如下,做工精良

- 例如輸入已被定義如下,做工精良

<div class="site input"> <i class="icon-pend icon calendar"></i> 
<input name="fromdate" type="text" data-datepicker="true" value="<?php if($_POST['fromdate'] <> ""){ echo $_POST['fromdate'];} else{echo $_GET['fromdate'];} ?>" placeholder="<?php echo Lang::$word->FROM;?>" id="fromdate" data-bind="<?php if($_POST['fromdate'] <> ""){ echo $_POST['fromdate'];} else{echo $_GET['fromdate'];} ?>"></div> 

- 帖子看起來像下面這樣

function createVerzuimServiceReport1() 
{ 
     var pg='<?php echo $_GET['pg'] ?>'; 
     var ipp='<?php echo $_GET['ipp'] ?>'; 
     var fromdate_submit=$('input[name="fromdate_submit"]').val(); 
     if(fromdate_submit=='') { 
      fromdate_submit='<?php echo $_GET['fromdate_submit'] ?>'; } 
     var enddate_submit=$('input[name="enddate_submit"]').val(); 
     if(enddate_submit=='') { 
      enddate_submit='<?php echo $_GET['enddate_submit'] ?>'; } 

     var date = new Date(Date.parse($('#fromdate').val())), 
       d = ("0" + date.getDate()).slice(-2), 
       m = ("0" + (date.getMonth() + 1)).slice(-2),    
       y = date.getFullYear(), 
       fDate = y + '-' + m + '-' + d;  
     var date = new Date(Date.parse($('#enddate').val())), 
       d = ("0" + date.getDate()).slice(-2), 
       m = ("0" + (date.getMonth() + 1)).slice(-2),    
       y = date.getFullYear(), 
       lDate = y + '-' + m + '-' + d; 

     var postData = { }; 
     postData.action = "postData"; 
     postData.createVerzuimServiceReport1 = "createVerzuimServiceReport1"; 
     postData.fromdate_submit = fDate; 
     postData.enddate_submit = lDate; 
     postData.studentsearchfield = $('#filter').val(); 

     $.ajax({ 
      type: "post", 
      url: "../plugins/registereddayvr/controller_verzuim_tot.php", 
      data: postData, 
      dataType: "json", 

       success: function(data) { 
        alert(postData.action + ' ' + postData.createVerzuimServiceReport1 + ' ' + postData.fromdate_submit + ' ' + postData.enddate_submit + ' ' + postData.studentsearchfield); 
        alert(postData.action + ' ' + postData.createVerzuimServiceReport1 + ' ' + postData.fromdate_submit + ' ' + postData.enddate_submit + ' ' + postData.studentsearchfield); 
        window.open("../plugins/registereddayvr/controller_verzuim_tot.php", "MsgWindow", "width=800, height=900");     

        if(data.fout) 
         alert('FOUT BERICHT: ' + data.bericht); 
       },   
       error: function (xhr, ajaxOptions, thrownError) { 
       alert(xhr.status); 
       alert(thrownError); 
      } 
     }); 
}; 

- PHP的看起來像這樣

if(isset($_POST['action']) && $_POST['action'] == "postData") { //if(isset($_GET['action']) && $_GET['action']== "postData") 
    $vals = array(
     'action' => $action, 
     'createVerzuimServiceReport1' => $createVerzuimServiceReport1, 
     'fromdate_submit' => $fromdate_submit, 
     'enddate_submit' => $enddate_submit, 
     'studentsearchfield' => $studentsearchfield 
    ); 
    // JSON encode er zijn params gevonden verstuur naar $.ajax success. 
    echo 'resultaat is: '.json_encode($vals); 
    exit; // zeker weten dat er niets anders is 
} 
     else { // zo is er toegang tot de fout bericht in jQuery 
     echo json_encode(array('fout' => TRUE, 'bericht' => 'Een probleem is ontstaan! Fout is: Er zijn geen parameters ontvangen in controller_verzuim_tot.php...', 'get' => $_GET, 'post' => $_POST,)); 
      exit; 
     } 

我已經檢查了所有在stackoverflow上發佈的答案,並閱讀了很多關於此主題的消息,並且我已經跟蹤了一些答案,但發件人($ _POST)不存在。我錯了什麼?感謝您的回答。

+0

我會建議你開始一個簡單的情況,而你還在學習。檢查這個問題的答案,例如http://stackoverflow.com/a/5004276/204610 – Michael

+0

邁克爾,我試過哪些也不起作用。 –

回答

0

您將URL中的操作作爲GET參數發送,但檢查爲POST參數。嘗試將檢查更改爲if ((isset($_GET['action']) && $_GET['action'] == "postData")...或向數據添加操作postData.action = 'postData';

正確的數據爲data: postData,postData爲allready對象;

+0

同樣在動作檢查中有錯誤,'isset'返回boolean - false/true,所以你不能將它與「postData」進行比較。 (ifset($ _ POST ['action'])&& if(() $ _POST ['action'] ==「postData」)..'關於你將如何發送動作參數 –

+0

我沒有看到你的php代碼的變化,所以我不知道爲什麼沒有改變。 –

+1

取消標記我的答案如果問題仍然存在,則接受。接受的答案意味着問題已解決。 –

0

postData已經是一個對象。因此,請勿使用{....}左右放入另一個對象。改變這一行

data: { postData }, 

data: postData,