简体   繁体   English

如何将 arguments 从 JS function 传递到 PHP 代码

[英]How to pass arguments from JS function to PHP code

I want to pass js function arguments "checklistNoteId" and "checklistNoteStatus" to PHP variable, so I can change things in database.我想将 js function arguments "checklistNoteId" 和 "checklistNoteStatus" 传递给 PHP 变量,所以我可以更改数据库中的内容。 What code should I write to pass those arguments?我应该写什么代码来通过那些 arguments? I have tried to do it but it doesn't work.我试过这样做,但它不起作用。 An example of my problem is bellow:我的问题的一个例子如下:

EDIT: I've tried this , but it didn't help me.编辑:我试过这个,但它没有帮助我。

JS CODE: 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 CODE: 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>

After this, the database remains untouched.在此之后,数据库保持不变。 (I am trying to set the note with id 26 to change isDone variable on button click.) The screen from database is here: (我正在尝试设置 id 为 26 的注释以更改按钮单击时的 isDone 变量。)来自数据库的屏幕在这里: 在此处输入图像描述

It's important to know the difference between client side code and server side code.了解客户端代码和服务器端代码之间的区别很重要。 PHP is server side, which means it will run on the server before it's sent to the client. PHP 是服务器端,这意味着它会在发送到客户端之前在服务器上运行。 Javascript is client side, which means it will run when the client receives the code. Javascript 是客户端,这意味着它会在客户端收到代码时运行。 Because of this, javascript can never write anything directly to PHP because the PHP script has already been run before the client even receives the webpage.因此,javascript 永远不能直接向 PHP 写入任何内容,因为 PHP 脚本在客户端甚至接收到网页之前就已经运行了。

In order to send something client side back to the server, you will need to send a Request.为了将客户端发送回服务器,您需要发送一个请求。 The most common way to do this is with a form element with a method=POST and an action pointed to a php file you want to receive.最常见的方法是使用带有method=POSTform元素和指向您要接收的 php 文件的action Here is a simple example: https://www.w3schools.com/tags/att_form_method.asp这是一个简单的例子: https://www.w3schools.com/tags/att_form_method.asp

If you want it to run in the background, I would recommend looking up AJAX requests and how to perform those instead.如果您希望它在后台运行,我建议您查找 AJAX 请求以及如何执行这些请求。 It's the same concept, but there is a little more to them.这是相同的概念,但它们还有更多。

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

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