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

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

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);

?>

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

  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:

function insertNewRecord(data) {

    fetch(&#39;/configs/database/add.php&#39;, {
        method: &#39;POST&#39;,
        headers: {
          &#39;Content-Type&#39;: &#39;application/x-www-form-urlencoded&#39;
        },
        body: `name=${data.name}&amp;contact=${data.contact}&amp;delivery=${data.delivery}&amp;value=${data.value}`
    })
    .then(function(response) {
        if (response.ok) {
            return response.text();
        }
        throw new Error(&#39;Error in the request.&#39;);
    })

    var table = document.getElementById(&quot;employeeList&quot;).getElementsByTagName(&#39;tbody&#39;)[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 = `&lt;a onClick=&quot;onEdit(this)&quot;&gt;Edit&lt;/a&gt;
                       &lt;a onClick=&quot;onDelete(this)&quot;&gt;Delete&lt;/a&gt;`;
}

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

&lt;?php 

    $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;;  
    $CurPageURL = $protocol . $_SERVER[&#39;HTTP_HOST&#39;] . $_SERVER[&#39;REQUEST_URI&#39;];  
    $parts = parse_url($CurPageURL);

    if(count($parts) &lt;= 3) return;

    parse_str($parts[&#39;query&#39;], $query);

    $conn = OpenCon();

    $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;;
    $result = $conn-&gt;query($sql);

?&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:

确定