gpt4 book ai didi

javascript - 创建动态表单元素时没有 POST 数据

转载 作者:行者123 更新时间:2023-11-28 05:26:39 24 4
gpt4 key购买 nike

首先,我不是开发人员,所以这种东西有点超出我的舒适区。

我试图找出为什么在提交表单时没有收到任何发布数据。我通过按钮使用一点 JavaScript 来创建动态表单元素并提交它们。

我有以下 html 片段:

       <form id="admin_approvals" method="post" action="process_request.php">
<table>
<tr><th>Task</th><th>Status</th><th colspan="3">Action</th></tr>
<tr>
<td>New phone purchase</td>
<td>WAITING APPROVAL</td>
<td><button type="button" id="approve" onclick="actionRequest('APPROVE',94)">APPROVE</button></td>
<td><button type="button" id="reject" onClick="actionRequest('REJECT',94)">REJECT</button></td>
<td><button type="button" id="delete" onClick="actionRequest('DELETE',94)">DELETE</button></td>
</tr>
</table>
</form>

JavaScript 看起来像这样:

function actionRequest(keyword,task_id) {

var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("action",keyword);
input.setAttribute("task",task_id);

document.getElementById("admin_approvals").appendChild(input);
document.getElementById("admin_approvals").submit();
}

PHP 这样做只是为了调试目的:

print_r($_POST);

单击按钮后我看到的只是一个空白数组:

Array( )

我尝试在 Javascript 中使用 getAttribute 进行调试,并且正在设置值。只是看起来没有数据被 POST'ed。

最佳答案

问题

问题不在于输入是动态附加或创建的。真正的问题是您的输入没有 name 属性。

解决方案

要修复此问题,只需在 JavaScript 中设置名称属性即可。

input.setAttribute("name", "test");

来源

Both GET and POST create an array (e.g. array( key => value, key2 => value2, key3 => value3, ...)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user.

http://www.w3schools.com/php/php_forms.asp

关于javascript - 创建动态表单元素时没有 POST 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40093393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com