将所有值保存在“多选"中 [英] Saving all values in "multiple select"
问题描述
我在 wordpress 中有一个具有多重选择形式的元框.
im having a metabox in wordpress with a mutliple select form.
<select name="my_meta_box_select" id="my_meta_box_select" multiple="" style="width:300px; height:400px;">
<option value="red">Red
</option>
<option value="blue">Blue
</option>
</select>
到目前为止一切顺利,所选值已保存,我可以检索它,但我希望两个值都被保存.
So far so good, the selected value gets saved and I am able to retrieve it, however I wish for both values to be saved.
例如,我希望同时保存红色和蓝色并能够从前端检索它.有没有办法实现这一目标?还有比 select 更好的表单吗?
For example I wish to save both red AND blue and be able to retrieve it from the frontend. Is there any way to achieve this? Are there better forms than a select?
目的是让用户从一个选择字段选择页面到另一个选择字段,然后保存第二个选择字段.
The purpose is for a user to select pages from one select field to another and then save the second select field.
选择 1:
红色
蓝色
绿色
选择 2:
橙色
-按钮-
现在,如果用户从 select 1
中选择 red
和 blue
然后按下 -button-
这些值将添加到选择 2.当我现在按下更新页面时,我希望将所有值保存在选择 2 中.
Now if a user where to select red
and blue
from select 1
and then press -button-
the values will be added to select 2. When I now press update page I wish to save all the values in select 2.
这就是我从当前选择字段中保存的方式(但它只保存一个选定的值)
This is how i save from my current select field (but it only saves ONE selected value)
if( isset( $_POST['my_meta_box_select'] ) )
update_post_meta( $post_id, 'my_meta_box_select', esc_attr( $_POST['my_meta_box_select'] ) );
推荐答案
经常被忽视,超级简单.
Often overlooked, super simple.
name 属性需要允许通过 $_POST
作为数组发送多个选择.例如:
The name attribute needs to allow for multiple selections to be sent over $_POST
as an array. For example:
<select name="my_meta_box_select[]" id="my_meta_box_select" multiple="" style="width:300px; height:400px;">
<option value="red">Red
</option>
<option value="blue">Blue
</option>
</select>
注意名称中的 []
:name="my_meta_box_select[]"
这与 multiple
属性一起,将允许您的 $_POST
变量以数组的形式包含所有选择.也就是说,$_POST['my_meta_box_select']
不仅仅是一个简单的值,而是一个包含所有选择的数组.
This, alongside the multiple
attribute, will allow your $_POST
variable to contain all selections as an array. That said, $_POST['my_meta_box_select']
will not just be a simple value, but rather will be an array will all selections.
这篇关于将所有值保存在“多选"中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!