简体   繁体   English

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

[英]No POST data when creating dynamic form elements

First off, I'm no developer so this kind of stuff is a little out of my comfort zone. 首先,我不是开发人员所以这种东西有点超出我的舒适区。

I'm trying to figure out why I'm not getting any post data when a form is submitted. 我想弄清楚为什么我在提交表单时没有收到任何帖子数据。 I'm using a little javascript via a button, to create dynamic form elements and submitting them. 我通过按钮使用一些小的javascript来创建动态表单元素并提交它们。

I have the following html snippet: 我有以下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>

The javascript looks like this: 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();
}

And the php just does this for debugging purposes: 而php只是为了调试目的而这样做:

print_r($_POST);

All I'm seeing after clicking the button is a blank array: 单击按钮后我看到的只是一个空白数组:

Array( )

I've tried debugging by using getAttribute in the Javascript, and the values are being set. 我已经尝试在Javascript中使用getAttribute进行调试,并且正在设置值。 It's just that it looks like no data is being POST'ed. 只是看起来没有数据正在POST。

Problem 问题

The problem is not that the input is appended or created dynamically. 问题不在于动态附加或创建输入。 The real problem is that your input has no name attribute. 真正的问题是你的输入没有name属性。

Solution

To fix it, simply set a name attribute in your JavaScript. 要修复它,只需在JavaScript中设置name属性即可。

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

Source 资源

Both GET and POST create an array (eg array( key => value, key2 => value2, key3 => value3, ...)). GET和POST都创建一个数组(例如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 http://www.w3schools.com/php/php_forms.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM