英文:
PHP script doesn't receive - or doesn't read - POST JavaScript
问题
以下是翻译好的内容:
我试图将一个主体作为查询发送到一个PHP脚本,但当我将信息视为“$query['param']”时,此信息返回为未定义。
我用于发送信息的JavaScript代码如下:
function insertNewRecord(data) {
fetch('/configs/database/add.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `name=${data.name}&contact=${data.contact}&delivery=${data.delivery}&value=${data.value}`
})
.then(function(response) {
if (response.ok) {
return response.text();
}
throw new Error('请求错误。');
})
var table = document.getElementById("employeeList").getElementsByTagName('tbody')[0];
var newRow = table.insertRow(table.length);
cell1 = newRow.insertCell(0);
cell1.innerHTML = data.name;
cell2 = newRow.insertCell(1);
cell2.innerHTML = data.contact;
cell3 = newRow.insertCell(2);
cell3.innerHTML = data.delivery;
cell4 = newRow.insertCell(3);
cell4.innerHTML = data.value;
cell4 = newRow.insertCell(4);
cell4.innerHTML = '<a onClick="onEdit(this)">编辑</a>' +
'<a onClick="onDelete(this)">删除</a>';
}
我用于接收请求的PHP脚本如下:
<?php
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$CurPageURL = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$parts = parse_url($CurPageURL);
if(count($parts) <= 3) return;
parse_str($parts['query'], $query);
$conn = OpenCon();
$sql = "INSERT INTO users (name, contact, delivery, value) VALUES (" . $query['name'] . ", " . $query['contact'] . ", " . $query['delivery'] . ", " . $query['value'] . ")";
$result = $conn->query($sql);
?>
要修复这个问题,你可以尝试以下几个步骤:
- 确保你的JavaScript代码中的
fetch
请求成功发送到了PHP脚本。 - 在PHP脚本中,确保
$query['name']
、$query['contact']
、$query['delivery']
和$query['value']
都包含在请求中,并且它们的值是正确的。 - 确保PHP脚本中的数据库连接(
OpenCon()
函数)正常工作,且没有错误。 - 如果问题仍然存在,可以在JavaScript代码中使用
console.log
输出来检查数据是否正确发送到PHP脚本,并在PHP脚本中使用var_dump
或error_log
来检查接收到的数据。
请确保你的代码中没有拼写错误,并根据错误消息来排除问题。
英文:
I'm trying to send a body as a query to a PHP script, but when I treat the information as "$query['param']", this information is returned as undefined.
The JavaScript code I'm using to send the information is as follows:
function insertNewRecord(data) {
fetch('/configs/database/add.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `name=${data.name}&contact=${data.contact}&delivery=${data.delivery}&value=${data.value}`
})
.then(function(response) {
if (response.ok) {
return response.text();
}
throw new Error('Error in the request.');
})
var table = document.getElementById("employeeList").getElementsByTagName('tbody')[0];
var newRow = table.insertRow(table.length);
cell1 = newRow.insertCell(0);
cell1.innerHTML = data.name;
cell2 = newRow.insertCell(1);
cell2.innerHTML = data.contact;
cell3 = newRow.insertCell(2);
cell3.innerHTML = data.delivery;
cell4 = newRow.insertCell(3);
cell4.innerHTML = data.value;
cell4 = newRow.insertCell(4);
cell4.innerHTML = `<a onClick="onEdit(this)">Edit</a>
<a onClick="onDelete(this)">Delete</a>`;
}
And the PHP script I'm using to receive the request is as follows:
<?php
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$CurPageURL = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$parts = parse_url($CurPageURL);
if(count($parts) <= 3) return;
parse_str($parts['query'], $query);
$conn = OpenCon();
$sql = "INSERT INTO users (name, contact, delivery, value) VALUES (" . $query['name'] . ", " . $query['contact'] . ", " . $query['delivery'] . ", " . $query['value'] . ")";
$result = $conn->query($sql);
?>
What should I do/change to fix this problem? I'm trying to do a CRUD in PHP, but this problem has been disturbing my work and I'm not able to solve it myself.
答案1
得分: 1
你要查找的数据在$_POST
中,但你使用parse_str
来从URL中读取值。这是代码中的问题。
在$_POST
中检查POST数据,例如$_POST["name"]
。
表单已经以POST方式提交,数据只会出现在URL中,如果你使用默认的GET方法来获取所有细节的URL。
英文:
The data you are looking for is in $_POST but you use parse_str to read the values out of the URL. That is the problem in the code.
Check for post data in $_POST
. Like $_POST["name"]
.
The form is POSTed and the data would only be in the URL if you did a fetch of a URL with all the details using the default GET method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论