2017-07-27 158 views
0

所以我有這樣的jQuery:jQuery的AJAX POST沒有將數據傳遞到PHP腳本

$("#dropbin").droppable(
    { 
    accept: '#dragme', 
    hoverClass: "drag-enter", 
    drop: function(event) 
    { 
     var noteid = "<?=isset($_POST['noteid']) ? $_POST['noteid'] : "" ?>"; 

     if (confirm('Delete the note?')==true) 
     { 
     $('#dragme').hide(); 
     debugger 
     $.ajax({ 
      type: 'POST', 
      data: noteid, 
      datatype: 'json', 
      url: 'deleteNote.php', 
      success: function(result) 
       { 
        alert("Success"); 
       } 
     }); 

     window.location = "http://discovertheplanet.net/general_notes.php"; 
     } 
     else 
     { 
     window.location = "http://discovertheplanet.net/general_notes.php"; 
     } 
    } 
    }); 

,幷包括此網址:url: 'deleteNote.php',

在deleteNote.php

<?php 

include "connectionDetails.php"; 

?> 

<?php 

if (isset($_POST['noteid'])) 
{ 
    // $noteid2 = $_POST['noteid1']; 

    echo "You finally hit this bit, congratulations..."; 

    // $stmt = "UPDATE Notes SET Deleted = GETDATE() WHERE NoteID = (?)"; 
    // $params = $noteid2; 

    // $stmt = sqlsrv_query($conn, $stmt, $params); 

    // if ($stmt === false) 
    // { 
    // die(print_r(sqlsrv_errors(), true)); 
    // } 

} 

else 
{ 
    echo "No Data"; 
} 


?> 

現在即使在URL中運行/deleteNote.php?noteid=25它也會觸發我的PHP的「無數據」部分。

當我在調試程序中運行時,它用NoteID填充變量noteid,以便該位可以工作,但PHP文件說它沒有設置?

+0

當您運行'/deleteNote.php?noteid = 25'時,它將運行「no data」,因爲這是一個GET設置,而不是您的代碼正在尋找的POST。 – IsThisJavascript

+0

你是否收到警報(「成功」);順便一提? – IsThisJavascript

+0

是的,我得到了成功的警報,編輯:我也試過GET以及我不知道,並沒有返回任何數據 –

回答

1

讓我們來看看在你的Ajax調用:

$.ajax({ 
     type: 'POST', 
     data: noteid, 
     datatype: 'json', 
     url: 'deleteNote.php', 
     success: function(result) 
      { 
       alert("Success"); 
      } 
    }); 

看起來不錯,但你沒有ID發送POST數據,你只是發送值。試試這個。

$.ajax({ 
     type: 'POST', 
     data: { 
      noteid: noteid 
     }, 
     datatype: 'json', 
     url: 'deleteNote.php', 
     success: function(result) 
      { 
       alert("Success"); 
      } 
    }); 
+0

我做了這個改動,但它仍然不起作用,仍然擊中無數據部分 –

+0

您的deletenote.php仍然有'if(isset($ _ POST ['noteid']))'? – IsThisJavascript

+0

是的,我也注意到它現在沒有打我的調試器? –