简体   繁体   English

'意外的标记 ;' 尝试在jQuery AJAX请求中传递PHP变量时

[英]'Unexpected token ;' when trying to pass PHP variable in jQuery AJAX request

So im trying to run a PHP script that sets a deleted field in the database to poplulate if you drag a certain text element to the droppable area. 因此,如果您将某个文本元素拖到可放置区域,则尝试运行一个PHP脚本,该脚本将数据库中已删除的字段设置为填充。

At the moment i have this droppable area : 目前,我有这个可放置区域

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

and this draggable text : 和这个可拖动的文本

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

The area im having an issue with is this: 我有问题的地区是这样的:

$("#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";
      }
    }
  });

EDIT: The line i get the error on is: 编辑:我得到错误的行是:

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

Im currently getting an "Unexpected token ;" 我目前正在获取“意外令牌”; and its stopping the droppable from working. 并阻止了可投放对象的工作。

Just a side note, if i run it without the variables it hits everything apart from: 只是附带说明一下,如果我在没有变量的情况下运行它,那么除以下内容外,其他所有内容都将受到影响:

url: 'deleteNote.php',

Also inside deleteNote.php is this incase it helps: 同样在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";
}


?>

(I deliberatley don't have deletedby in the database just yet, ignore that) (我deliberatley还没有在数据库中删除by,请忽略它)

Could anyone possibly help me to get this to work? 有人可以帮助我使它正常工作吗?

Try to add quotes in these lines and add php after <? 尝试在这些行中添加引号,并在<?之后添加php <? in second line: 在第二行:

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

OR 要么

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM