我有一个js函数,它传递一个fileid以通过php脚本删除。 我不知道如何将JavaScript参数从JavaScript转换为PHP变量。将JavaScript函数参数转换为php变量
这是我迄今所做的:
<script>
function deleteFile(file)
{
var r = confirm("Are you sure you want to delete this file?");
if (r === true)
{
<?php
$idfile = file; // How to convert it??
unlink(mysql_result(
mysql_query("SELECT filepath FROM
file where idfile='$idfile'"), 0, 0))
or die("Could not delete file");
mysql_query("DELETE FROM file WHERE fileid=$idfile")
or die("Cannot delete file");
echo "File has been deleted successfully.";
?>
}
}
</script>
我有一个按钮也:
echo "<button onclick=\"deleteFile($fileid)\">Delete</button>";
UPDATE
function deleteFile(file)
{
var r = confirm("Are you sure you want to delete this file?");
if (r === true)
{ // doesn't go to deletefile.php
$.ajax({
url: "/deletefile.php",
method: 'POST',
data: {
id: file
}
});
}
}
一个是服务器端,一个是客户端。没有转换它。你必须打电话给服务器。 – Danny
看起来你对服务器端和客户端代码的工作方式感到困惑。服务器端代码无法知道客户端发生了什么,除非通过使用像ajax这样的技术告诉服务器端。 –
PHP正在构建页面时在服务器上运行。 PHP完成后,Javascript在客户端上运行。您无法将JS变量转换为PHP变量,因为PHP不再运行。您需要通过提交表单,关联链接或使用AJAX将某些内容发送到服务器。 – Barmar