Passing extra data in $_POST vars
I need to pass an id along with a form field e.g
<input name="__field_name" value="123开发者_开发问答4" />
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
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']
.
You could add a hidden field which contains the input field name value.
<input type="text" name="field_1" />
<input type="hidden" name="field_1_name"/>
You need to add a hidden form field which contains the Id of the first field. You can name it as field1_ID or something.
<input type="text" name="first_field" value="As_Entered_By_User"/>
<input type="hidden" name="first_field_id" value="id_first_field"/>
Or if you are familiar with Javascript, You can post it using javascript with single form field putting the id as an attribute.
<input type="text" name="first_field" id="first_field_id" value="as_enteredBy_user"/>
<script>var id_to_post=document.form1.first_field.id;</script>
here form1 is the name of the form containing the input box.
精彩评论