dsqbh42082 2013-11-17 18:46
浏览 77

从<select multiple>框中输入多个选择到数据库中

I have the bit of code below and I would like uses to select multiple options in the "select multiple" box and for these all to be added into the database. At the moment only 1 of the values that is selected is entered. I am a noob with this sort of thing so an in need of help to progress with the project.

Thanks for any help!

function ProjectTheme_get_categories($taxo, $selected = "", $include_empty_option = "",     $ccc = "")
{
$args = "orderby=name&order=ASC&hide_empty=0&parent=0";
$terms = get_terms( $taxo, $args );

$ret = '<select multiple size="20" name="'.$taxo.'_cat" class="'.$ccc.'" id="scrollselect     '.$ccc.'">';
if(!empty($include_empty_option)) $ret .= "<option value=''>".$include_empty_option."</o    ption>";

if(empty($selected)) $selected = -1;

foreach ( $terms as $term )
{
$id = $term->term_id;

$ret .= '<option '.($selected == $id ? "selected='selected'" : " " ).' value="'.$id.'">'.$term->name.'</option>';

$args = "orderby=name&order=ASC&hide_empty=0&parent=".$id;
$sub_terms = get_terms( $taxo, $args ); 

foreach ( $sub_terms as $sub_term )
{
    $sub_id = $sub_term->term_id; 
    $ret .= '<option '.($selected == $sub_id ? "selected='selected'" : " " ).' value="'.$sub_id.'">&nbsp; &nbsp;|&nbsp;  '.$sub_term->name.'</option>';

    $args2 = "orderby=name&order=ASC&hide_empty=0&parent=".$sub_id;
    $sub_terms2 = get_terms( $taxo, $args2 );   

    foreach ( $sub_terms2 as $sub_term2 )
    {
        $sub_id2 = $sub_term2->term_id; 
        $ret .= '<option '.($selected == $sub_id2 ? "selected='selected'" : " " ).' value="'.$sub_id2.'">&nbsp; &nbsp; &nbsp; &nbsp;|&nbsp; 
         '.$sub_term2->name.'</option>';

    }
}

}

$ret .= '</select>';

return $ret;

}

Here is another section of the code where the user interacts with that form

<li><h2><?php echo __('Category', 'ProjectTheme'); ?>:</h2>
        <p><?php    echo ProjectTheme_get_categories("project_cat",  
        !isset($_POST['project_cat_cat']) ? (is_array($cat) ? $cat[0]->term_id : "") : $_POST['project_cat_cat']
        , __("Select Category","ProjectTheme"), "do_input"); ?></p>
    </li>

Additional code that handles the submit function of the form

do_action('ProjectTheme_post_new_post_post',$pid);

if(isset($_POST['project_submit1']))
{


    $project_title          = trim($_POST['project_title']);
    $project_description    = nl2br($_POST['project_description']);
    $project_category       = $_POST['project_cat_cat'];
    $project_location       = trim($_POST['project_location_cat']);
    $project_tags           = trim($_POST['project_tags']);
    $price                  = trim($_POST['price']);
    $project_location_addr  = trim($_POST['project_location_addr']);
    $end                    = trim($_POST['ending']); 

    update_post_meta($pid, 'finalised_posted', '0');

    //--------------------------------

    $projectOK = 1;

    if(empty($project_title))       { $projectOK = 0; $MYerror['title']         = __('You cannot leave the project title blank!','ProjectTheme'); }
    if(empty($project_category)) { $projectOK = 0; $MYerror['cate']     = __('You cannot leave the project category blank!','ProjectTheme'); }
    if(empty($project_description)) { $projectOK = 0; $MYerror['description']   = __('You cannot leave the project description blank!','ProjectTheme'); }

    //--------------------------------


    $project_category2  = $project_category;

    $my_post = array();

    $my_post['post_title']      = $project_title;
    $my_post['post_status']     = 'draft';
    $my_post['ID']              = $pid;
    $my_post['post_content']    = $project_description;

    $term = get_term( $project_category, 'project_cat' );   
    $project_category = $term->slug;

    $term = get_term( $project_location, 'project_location' );  
    $project_location = $term->slug;

    wp_update_post( $my_post );
    wp_set_object_terms($pid, array($project_category),'project_cat');
    wp_set_object_terms($pid, array($project_location),'project_location');

    wp_set_post_tags( $pid, $project_tags);
  • 写回答

2条回答 默认 最新

  • dongliao6777 2013-11-17 18:59
    关注

    Append a [] to Select Tag's name like this <select name="taxo_cat[]" multiple> and in your php code you can access array of values by calling $_POST['taxo_cat']

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错