duancanjiu3754 2011-08-25 06:25
浏览 104
已采纳

在$ _POST变量中传递额外数据

I need to pass an id along with a form field e.g

<input name="__field_name" value="1234" />

this only passes the name and value as a key => value pair. i need to keep the name (dynamically entered by the user) and value intact for later use, but i also need to pass an ID along with this var.

how can i do this cleanly? i was thinking putting it in the name and doing a regex to seperate it e.g.

__field_name__ID  

although this seems messy...

points to consider:

  • there are allot of post variables that are generated by the CMS (wordpress) that i wont use
  • name must be retained in original format along with value
  • 写回答

3条回答 默认 最新

  • doujiufutaog59220 2011-08-25 06:28
    关注

    Why not submit the data as an array?

    Instead of calling your field __field_name__id or some mess, use the facilities PHP provides: Call your input field field_name[id] and when the form is posted back to the server, PHP's $_POST array will have a sub-array called field_name which contains the key=>value mappings you'd mentioned.

    If you have two such fields you want to tie together, use the following:

    <input type="text" name="myFields[id]" />
    <input type="text" name="myFields[name]" />
    

    And on postback, PHP will provide you with a $_POST['myFields']['id'] and $_POST['myFields']['name'].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?