简体   繁体   中英

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.

I have the following html snippet:

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

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:

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. It's just that it looks like no data is being POST'ed.

Problem

The problem is not that the input is appended or created dynamically. The real problem is that your input has no name attribute.

Solution

To fix it, simply set a name attribute in your JavaScript.

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

Source

Both GET and POST create an array (eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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