2017-07-27 154 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

是的,我也注意到它现在没有打我的调试器? –