gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:07:31 24 4
gpt4 key购买 nike

因此,我尝试运行一个 PHP 脚本,如果您将某个文本元素拖动到可放置区域,该脚本将数据库中已删除的字段设置为填充。

目前我有这个可放置区域:

<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'];} ?>;

我目前收到一个“意外 token ;”,它阻止了 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";
}


?>

(我故意在数据库中还没有deleteby,忽略它)

有人可以帮助我让它发挥作用吗?

最佳答案

尝试在这些行中添加引号并添加 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'] : "" ?>";

关于javascript - '意外的标记 ;'当尝试在 jQuery AJAX 请求中传递 PHP 变量时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45348289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com