'意外的令牌;'当试图在jQuery AJAX请求中传递PHP变量时

时间:2017-07-27 10:34:29

标签: javascript php jquery html ajax

因此,我试图运行一个PHP脚本,如果将某个文本元素拖到可放置区域,则会将数据库中的已删除字段设置为poplulate。

目前我有可放置区

<div class="dropbin" id="dropbin" >
    <span class="fa fa-trash-o noSelect hover-cursor" style="font-size: 20pt; line-height: 225px;">&nbsp;</span>
</div>

以及可拖动文字

<div id='dragme' data-toggle='modal' data-target='#editNoteNameModal' class='display-inline'>" . $data['NoteName'] . "</div>

我遇到问题的区域是:

$("#dropbin").droppable
  ({
    accept: '#dragme', 
    hoverClass: "drag-enter",
    drop: function(event) 
    {
      var noteid = <?php if(isset($_POST['noteid'])){ echo $_POST['noteid'];} ?>;
      var deletedby = <? if(isset($_SESSION['username'])){ echo $_SESSION['username'];} ?>
      var data = {noteid1: noteid, deletedby1: deletedby};

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

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

编辑:我收到错误的行是:

var noteid = <?php if(isset($_POST['noteid'])){ echo $_POST['noteid'];} ?>;

我目前正在获取一个&#34;意外的令牌;&#34; 并阻止了droppable工作。

只是旁注,如果我在没有变量的情况下运行它,它会触及除了以下所有内容:

url: 'deleteNote.php',

同样在deleteNote.php里面,这有助于:

<?php

include "connectionDetails.php";

?>

<?php

if (isset($_POST['noteid1'], $_POST['deletedby1'])) 
{
    $noteid2 = $_POST['noteid1'];
    $deletedby2 = $_POST['deletedby1'];

    // echo "Hello...". $noteid;

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

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

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

else
{
    echo "No Data";
}


?>

(我还没有在数据库中删除过,忽略了这一点)

有没有人可以帮助我让这个工作?

1 个答案:

答案 0 :(得分:5)

尝试在这些行中添加引号,并在php后面的第二行添加<?

var noteid = "<?php if(isset($_POST['noteid'])){ echo $_POST['noteid'];} ?>";
var deletedby = "<?php if(isset($_SESSION['username'])){ echo $_SESSION['username'];} ?>";

var noteid = "<?=isset($_POST['noteid']) ? $_POST['noteid'] : "" ?>";
var deletedby = "<?=isset($_SESSION['username']) ? $_SESSION['username'] : "" ?>";