PHP脚本未接收 – 或未读取 – POST JavaScript。

huangapple go评论93阅读模式
英文:

PHP script doesn't receive - or doesn't read - POST JavaScript

问题

以下是翻译好的内容:

我试图将一个主体作为查询发送到一个PHP脚本,但当我将信息视为“$query['param']”时,此信息返回为未定义。

我用于发送信息的JavaScript代码如下:

  1. function insertNewRecord(data) {
  2. fetch('/configs/database/add.php', {
  3. method: 'POST',
  4. headers: {
  5. 'Content-Type': 'application/x-www-form-urlencoded'
  6. },
  7. body: `name=${data.name}&contact=${data.contact}&delivery=${data.delivery}&value=${data.value}`
  8. })
  9. .then(function(response) {
  10. if (response.ok) {
  11. return response.text();
  12. }
  13. throw new Error('请求错误。');
  14. })
  15. var table = document.getElementById("employeeList").getElementsByTagName('tbody')[0];
  16. var newRow = table.insertRow(table.length);
  17. cell1 = newRow.insertCell(0);
  18. cell1.innerHTML = data.name;
  19. cell2 = newRow.insertCell(1);
  20. cell2.innerHTML = data.contact;
  21. cell3 = newRow.insertCell(2);
  22. cell3.innerHTML = data.delivery;
  23. cell4 = newRow.insertCell(3);
  24. cell4.innerHTML = data.value;
  25. cell4 = newRow.insertCell(4);
  26. cell4.innerHTML = '<a onClick="onEdit(this)">编辑</a>' +
  27. '<a onClick="onDelete(this)">删除</a>';
  28. }

我用于接收请求的PHP脚本如下:

  1. <?php
  2. $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  3. $CurPageURL = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  4. $parts = parse_url($CurPageURL);
  5. if(count($parts) <= 3) return;
  6. parse_str($parts['query'], $query);
  7. $conn = OpenCon();
  8. $sql = "INSERT INTO users (name, contact, delivery, value) VALUES (" . $query['name'] . ", " . $query['contact'] . ", " . $query['delivery'] . ", " . $query['value'] . ")";
  9. $result = $conn->query($sql);
  10. ?>

要修复这个问题,你可以尝试以下几个步骤:

  1. 确保你的JavaScript代码中的fetch请求成功发送到了PHP脚本。
  2. 在PHP脚本中,确保$query['name']$query['contact']$query['delivery']$query['value']都包含在请求中,并且它们的值是正确的。
  3. 确保PHP脚本中的数据库连接(OpenCon()函数)正常工作,且没有错误。
  4. 如果问题仍然存在,可以在JavaScript代码中使用console.log输出来检查数据是否正确发送到PHP脚本,并在PHP脚本中使用var_dumperror_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:

  1. function insertNewRecord(data) {
  2. fetch(&#39;/configs/database/add.php&#39;, {
  3. method: &#39;POST&#39;,
  4. headers: {
  5. &#39;Content-Type&#39;: &#39;application/x-www-form-urlencoded&#39;
  6. },
  7. body: `name=${data.name}&amp;contact=${data.contact}&amp;delivery=${data.delivery}&amp;value=${data.value}`
  8. })
  9. .then(function(response) {
  10. if (response.ok) {
  11. return response.text();
  12. }
  13. throw new Error(&#39;Error in the request.&#39;);
  14. })
  15. var table = document.getElementById(&quot;employeeList&quot;).getElementsByTagName(&#39;tbody&#39;)[0];
  16. var newRow = table.insertRow(table.length);
  17. cell1 = newRow.insertCell(0);
  18. cell1.innerHTML = data.name;
  19. cell2 = newRow.insertCell(1);
  20. cell2.innerHTML = data.contact;
  21. cell3 = newRow.insertCell(2);
  22. cell3.innerHTML = data.delivery;
  23. cell4 = newRow.insertCell(3);
  24. cell4.innerHTML = data.value;
  25. cell4 = newRow.insertCell(4);
  26. cell4.innerHTML = `&lt;a onClick=&quot;onEdit(this)&quot;&gt;Edit&lt;/a&gt;
  27. &lt;a onClick=&quot;onDelete(this)&quot;&gt;Delete&lt;/a&gt;`;
  28. }

And the PHP script I'm using to receive the request is as follows:

  1. &lt;?php
  2. $protocol = ((!empty($_SERVER[&#39;HTTPS&#39;]) &amp;&amp; $_SERVER[&#39;HTTPS&#39;] != &#39;off&#39;) || $_SERVER[&#39;SERVER_PORT&#39;] == 443) ? &quot;https://&quot; : &quot;http://&quot;;
  3. $CurPageURL = $protocol . $_SERVER[&#39;HTTP_HOST&#39;] . $_SERVER[&#39;REQUEST_URI&#39;];
  4. $parts = parse_url($CurPageURL);
  5. if(count($parts) &lt;= 3) return;
  6. parse_str($parts[&#39;query&#39;], $query);
  7. $conn = OpenCon();
  8. $sql = &quot;INSERT INTO users (name, contact, delivery, value) VALUES (&quot; . $query[&#39;name&#39;] . &quot;, &quot; . $query[&#39;contact&#39;] . &quot;, &quot; . $query[&#39;delivery&#39;] . &quot;, &quot; . $query[&#39;value&#39;] . &quot;)&quot;;
  9. $result = $conn-&gt;query($sql);
  10. ?&gt;

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[&quot;name&quot;].
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.

huangapple
  • 本文由 发表于 2023年6月22日 11:13:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定