如何将参数从 JS 函数传递到 PHP 代码
How to pass arguments from JS function to PHP code
我想将 js 函数参数“checklistNoteId”和“checklistNoteStatus”传递给 PHP 变量,这样我就可以更改数据库中的内容。我应该编写什么代码来传递这些参数?我试过这样做,但没有用。我的问题的一个例子如下:
编辑:我试过 this,但没有帮助。
JS 代码:
checklistFinishedBtn.onclick = function(e){
let noteId = Number(e.target.parentNode.getAttribute('checklist-id'));
if (checklistArray.checklistItems[noteId].isDone){
changeChecklistNoteStatus(checklistArray.checklistItems[noteId].id, 0);
} else {
changeChecklistNoteStatus(checklistArray.checklistItems[noteId].id, 1);
}
}
PHP 代码:
<head>
<script>
function changeChecklistNoteStatus(checklistNoteId, checklistNoteStatus){
console.log(checklistNoteId); //this returns 26 (which is correct)
console.log(checklistNoteStatus); //this returns 1 (which is correct)
<?php
//if I set these values manually, it works
$status = "<script>document.write(checklistNoteStatus)</script>";
$id = "<script>document.write(checklistNoteId)</script>";
$sql = 'UPDATE checklist_notes SET isDone = :isDone WHERE id = :id';
$statement = $pdo->prepare($sql);
$statement->execute(['isDone' => $status,'id' => $id]);
?>
}
</script>
</head>
在此之后,数据库保持不变。 (我正在尝试设置 id 为 26 的注释以在单击按钮时更改 isDone 变量。)
来自数据库的屏幕在这里:
了解客户端代码和服务器端代码之间的区别很重要。 PHP 是服务器端,这意味着它将在发送到客户端之前先在服务器上 运行 。 Javascript是client side,也就是说当client收到code的时候会运行。因此,javascript 永远无法直接向 PHP 写入任何内容,因为 PHP 脚本在客户端甚至收到网页之前就已经 运行。
为了将客户端发送回服务器,您需要发送请求。最常见的方法是使用 form
元素,其中 method=POST
和 action
指向您要接收的 php 文件。这是一个简单的例子:https://www.w3schools.com/tags/att_form_method.asp
如果您希望它在后台 运行,我建议您查看 AJAX 请求以及如何执行这些请求。这是相同的概念,但对他们来说还有更多。
我想将 js 函数参数“checklistNoteId”和“checklistNoteStatus”传递给 PHP 变量,这样我就可以更改数据库中的内容。我应该编写什么代码来传递这些参数?我试过这样做,但没有用。我的问题的一个例子如下:
编辑:我试过 this,但没有帮助。
JS 代码:
checklistFinishedBtn.onclick = function(e){
let noteId = Number(e.target.parentNode.getAttribute('checklist-id'));
if (checklistArray.checklistItems[noteId].isDone){
changeChecklistNoteStatus(checklistArray.checklistItems[noteId].id, 0);
} else {
changeChecklistNoteStatus(checklistArray.checklistItems[noteId].id, 1);
}
}
PHP 代码:
<head>
<script>
function changeChecklistNoteStatus(checklistNoteId, checklistNoteStatus){
console.log(checklistNoteId); //this returns 26 (which is correct)
console.log(checklistNoteStatus); //this returns 1 (which is correct)
<?php
//if I set these values manually, it works
$status = "<script>document.write(checklistNoteStatus)</script>";
$id = "<script>document.write(checklistNoteId)</script>";
$sql = 'UPDATE checklist_notes SET isDone = :isDone WHERE id = :id';
$statement = $pdo->prepare($sql);
$statement->execute(['isDone' => $status,'id' => $id]);
?>
}
</script>
</head>
在此之后,数据库保持不变。 (我正在尝试设置 id 为 26 的注释以在单击按钮时更改 isDone 变量。)
来自数据库的屏幕在这里:
了解客户端代码和服务器端代码之间的区别很重要。 PHP 是服务器端,这意味着它将在发送到客户端之前先在服务器上 运行 。 Javascript是client side,也就是说当client收到code的时候会运行。因此,javascript 永远无法直接向 PHP 写入任何内容,因为 PHP 脚本在客户端甚至收到网页之前就已经 运行。
为了将客户端发送回服务器,您需要发送请求。最常见的方法是使用 form
元素,其中 method=POST
和 action
指向您要接收的 php 文件。这是一个简单的例子:https://www.w3schools.com/tags/att_form_method.asp
如果您希望它在后台 运行,我建议您查看 AJAX 请求以及如何执行这些请求。这是相同的概念,但对他们来说还有更多。