在我的 JS 代码中,我在 html 页面上接收 3 个输入并将它们保存到本地存储。然后我想将这些变量发送到 php 以便将它们保存到我的数据库中。无论我多么努力地尝试,没有使用 ajax、jquery 等的教程允许我在我的 php 代码中成功发布和回显来自 javascript 的变量。我认为下面的代码没有理由不回显变量,但事实并非如此。
完整代码:https://codeshare.io/ayvK9e
精确的 PHP 元素(现在只是尝试发送普通变量,因为它仍然无法工作”
PHP:
foreach($_POST as $post_var){
echo($post_var);
}
JS:
const xhr = new XMLHttpRequest();
xhr.onload = function(){
const serverResponse = document.getElementById("serverResponse");
serverResponse.innerHTML = this.responseText
};
xhr.open("POST", "eDBase.php");
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send("name=dominic&message=bumbaclaat");
最佳答案
如果 $post 不起作用尝试使用输入 php $data = json_decode(file_get_contents('php://input'), true);
关于javascript - 将 js 变量发布到未在任何方法中运行的 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59820708/