在前端发布表单中保存两个下拉列表中的两个类别

时间:2012-07-05 作者:Andrew

那么,交易如下:

我有两个类别下拉列表,第一个用于父类别,第二个用于子类别。它们形成一个链,以便在选择父类别时,第二个下拉列表将填充其子类别。以下是表单本身的代码:

    <script type="text/javascript">
    $(function()
    {
    $(\'#main_cat\').change(function()
    {
        var $mainCat=$(\'#main_cat\').val();

        // call ajax
        $("#sub_cat").empty();
        $.ajax
        (
            {
                url:"<?php bloginfo(\'wpurl\'); ?>/wp-admin/admin-ajax.php",     
                type:\'POST\',
                data:\'action=my_special_ajax_call&main_catid=\' + $mainCat,

                success:function(results)
                {
                    //  alert(results);
                    $("#sub_cat").removeAttr("disabled");       
                    $("#sub_cat").append(results);  
                }
            }
        );                                    
    });
    });
    </script>
    <fieldset class="site-url" class="fieldset-first">
    <?php wp_dropdown_categories(\'show_count=0&selected=-1&hierarchical=1&depth=1&hide_empty=0&exclude=1&show_option_none=Main Categories&name=main_cat&taxonomy=category\');
    ?>
</fieldset>
<fieldset class="site-url" class="fieldset-first">
<select name="sub_cat" id="sub_cat" disabled="disabled"></select>
</fieldset>
下面是函数中的代码。php:

function implement_ajax() {
if(isset($_POST[\'main_catid\']))
    {
    $categories=  get_categories(\'child_of=\'.$_POST[\'main_catid\'].\'&hide_empty=0\'.\'&taxonomy=category\'.\'&show_count=0\');
      foreach ($categories as $cat) {
        $option .= \'<option value="\'.$cat->term_id.\'">\';
        $option .= $cat->cat_name;
        $option .= \' (\'.$cat->category_count.\')\';
        $option .= \'</option>\';
      }
      echo \'<option value="-1" selected="selected">Sub Categories</option>\'.$option;
    die();
    } // end if
}
add_action(\'wp_ajax_my_special_ajax_call\', \'implement_ajax\');
add_action(\'wp_ajax_nopriv_my_special_ajax_call\', \'implement_ajax\');
问题是,在像这样保存值时:

\'post_category\' =>   array($_POST[\'main_cat\']),
我将只为帖子保存父类别。我究竟如何才能同时保存父类别和子类别?请记住,每个下拉列表中只能选择一个类别,不能选择多个类别。

干杯

更新了以下全部帖子过账/保存代码:

<?php       
if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "new_post") {

    // Do some minor form validation to make sure there is content

    require_once(\'recaptchalib.php\');
        $privatekey="Oh dear I shouldn\'t have put my super private API key here for all to see!";

        $resp = recaptcha_check_answer ($privatekey,
                                        $_SERVER["REMOTE_ADDR"],
                                        $_POST["my_recapthca_challange_field"],
                                        $_POST["recaptcha_response_field"]);

if ($resp->is_valid) {

    if (isset ($_POST[\'title\'])) {
        $title =  $_POST[\'title\'];
        $_SESSION[\'form_values\'][\'title\'] = $_POST[\'title\'];
    } 

    if (isset ($_POST[\'site-link\'])) {
        $siteurl =  $_POST[\'site-link\'];
        $_SESSION[\'form_values\'][\'site-link\'] = $_POST[\'site-link\'];
    }

if (isset ($_POST[\'first-url\'])) {
        $firsturl =  $_POST[\'first-url\'];
        $_SESSION[\'form_values\'][\'first-url\'] = $_POST[\'first-url\'];
    }

if (isset ($_POST[\'price\'])) {
        $price =  $_POST[\'price\'];
        $_SESSION[\'form_values\'][\'price\'] = $_POST[\'price\'];
    }

    if (isset ($_POST[\'name\'])) {
        $devname =  $_POST[\'name\'];
        $_SESSION[\'form_values\'][\'name\'] = $_POST[\'name\'];   
    } 

    if (isset ($_POST[\'second-url\'])) {
        $secondurl =  $_POST[\'second-url\'];
         $_SESSION[\'form_values\'][\'second-url\'] = $_POST[\'second-url\'];
    } 

    if (isset ($_POST[\'country\'])) {
        $devcountry =  $_POST[\'country\'];
         $_SESSION[\'form_values\'][\'country\'] = $_POST[\'country\'];
    }

    if ($_POST[\'twitter\']=="Twitter Username" || $_POST[\'twitter\']=="Not Available")  {
        $twitter =  "Not Available";
         $_SESSION[\'form_values\'][\'twitter\'] = $twitter;
    }else  {
        $twitter = $_POST[\'twitter\'];
        $_SESSION[\'form_values\'][\'twitter\'] = $_POST[\'twitter\'];
    }

    $design =  $_POST[\'design\'];
       if ($design=="yes"){$design = "Yes, this is new!";}
        else {$design="No, this is old!";}


        $related =  $_POST[\'related\'];
       if ($related=="yes"){$related = "Yes, this is related!";}
        else {$related="No, this is not related!";}

$responds =  $_POST[\'responds\'];
       if ($responds=="yes"){$responds = "Yes";}
        else {$responds="No";}

$locale =  $_POST[\'locale\'];
       if ($locale=="yes"){$locale = "Yes";}
        else {$locale="No";}

$widgets =  $_POST[\'widgets\'];
       if ($widgets=="yes"){$widgets = "Yes";}
        else {$widgets="No";}

$skins =  $_POST[\'skins\'];
       if ($skins=="yes"){$skins = "Yes";}
        else {$skins="No";}

$contact =  $_POST[\'contact\'];
       if ($contact=="yes"){$contact = "Yes";}
        else {$contact="No";}

$press =  $_POST[\'press\'];
       if ($press=="yes"){$press = "Yes";}
        else {$press="No";}

$comm =  $_POST[\'comm\'];
       if ($comm=="yes"){$comm = "Yes";}
        else {$comm="No";}

$page =  $_POST[\'page\'];
       if ($page=="yes"){$page = "Yes";}
        else {$page="No";}

$psd =  $_POST[\'psd\'];
       if ($psd=="yes"){$psd = "Yes";}
        else {$psd="No";}

$format =  $_POST[\'format\'];
       if ($format=="yes"){$format = "Yes";}
        else {$format="No";}

$codes =  $_POST[\'codes\'];
       if ($codes=="yes"){$codes = "Yes";}
        else {$codes="No";}


     if ($_FILES[\'image\'][\'error\']==0) { 

    // ADD THE FORM INPUT TO $new_post ARRAY
    $new_post = array(
    \'post_title\'    =>   $title,
\'post_category\' =>   array($_POST[\'main_cat\']),
    \'post_status\'   =>   \'draft\',
    \'post_type\' =>   \'post\'
    );

    $pid = wp_insert_post($new_post);
    add_post_meta($pid, \'URL\', $siteurl, true);
    add_post_meta($pid, \'Name\', $name, true);
    add_post_meta($pid, \'First URL\', $firsturl, true);
    add_post_meta($pid, \'Country\', $country, true);
    add_post_meta($pid, \'Twitter\', $twitter, true);
    add_post_meta($pid, \'Redesign\', $redesign, true);
    add_post_meta($pid, \'Related\', $related, true);
add_post_meta($pid, \'Responds\', $responds, true);
add_post_meta($pid, \'Locale\', $locale, true);
add_post_meta($pid, \'Widgets\', $widgets, true);
add_post_meta($pid, \'Skins\', $skins, true);
add_post_meta($pid, \'Contact\', $contact, true);
add_post_meta($pid, \'Press\', $press, true);
add_post_meta($pid, \'Comm\', $comm, true);
add_post_meta($pid, \'Page\', $page, true);
add_post_meta($pid, \'PSD\', $psd, true);
add_post_meta($pid, \'Format\', $format, true);
add_post_meta($pid, \'Second URL\', $secondurl, true);
add_post_meta($pid, \'Price\', $price, true);
add_post_meta($pid, \'Codes\', $codes, true);

wp_set_post_categories($post_id, $_POST[\'main_cat\']);
    wp_set_post_tags($post_id, $_POST[\'post_tags\']);


    foreach ($_FILES as $file => $array) {
    $newupload = insert_attachment($file,$pid);

    }
    $imagedata = simplexml_load_string(get_the_post_thumbnail($pid));
    $post_img = $imagedata->attributes()->src;

    include_once("script/colors.inc.php");
    $image = $post_img;
    $ex=new GetMostCommonColors();
    $ex->image = $image;
    $colors=$ex->Get_Color();
    $how_many=10;
    $colors_key=array_keys($colors);
    $colortag = $colors_key[1];
    for ($i = 2; $i <= $how_many; $i++)
    {
    $colortag = $colortag.",".$colors_key[$i];
    }

    wp_set_post_tags($pid, $colortag);
     $_SESSION[\'error_message\'] = \'Submitted successfully.\';
     $_SESSION[\'form_values\'] = array();
     do_action(\'wp_insert_post\', \'wp_insert_post\');

    }

} else {
    if (isset ($_POST[\'title\'])) {
        $name =  $_POST[\'title\'];
        $_SESSION[\'form_values\'][\'title\'] = $_POST[\'title\'];
    } 

    if (isset ($_POST[\'site-link\'])) {
        $siteurl =  $_POST[\'site-link\'];
        $_SESSION[\'form_values\'][\'site-link\'] = $_POST[\'site-link\'];
    }

if (isset ($_POST[\'first-url\'])) {
        $firsturl =  $_POST[\'first-url\'];
        $_SESSION[\'form_values\'][\'first-url\'] = $_POST[\'first-url\'];
    }

if (isset ($_POST[\'price\'])) {
        $price =  $_POST[\'price\'];
        $_SESSION[\'form_values\'][\'price\'] = $_POST[\'price\'];
    }

    if (isset ($_POST[\'name\'])) {
        $name =  $_POST[\'name\'];
        $_SESSION[\'form_values\'][\'name\'] = $_POST[\'name\'];

    }

    if (isset ($_POST[\'second-url\'])) {
        $secondurl =  $_POST[\'dev-url\'];
         $_SESSION[\'form_values\'][\'second-url\'] = $_POST[\'second-url\'];
    }

    if (isset ($_POST[\'country\'])) {
        $country =  $_POST[\'country\'];
         $_SESSION[\'form_values\'][\'country\'] = $_POST[\'country\'];
    }

    if ($_POST[\'twitter\']=="Twitter Username" || $_POST[\'twitter\']=="Not Available")  {
        $twitter =  "Not Available";
         $_SESSION[\'form_values\'][\'twitter\'] = $twitter;
    }else  {
        $twitter = $_POST[\'twitter\'];
        $_SESSION[\'form_values\'][\'twitter\'] = $_POST[\'twitter\'];
    }

    $redesign =  $_POST[\'redesign\'];
       if ($redesign=="yes"){$redesign = "Yes, this is new!";}
        else {$redesign="No, this is old!";}


        $related =  $_POST[\'related\'];
       if ($related=="yes"){$related = "Yes, this is related!";}
        else {$related="No, this is not related!";}

$responds =  $_POST[\'responds\'];
       if ($responds=="yes"){$responds = "Yes";}
        else {$responds="No";}

$locale =  $_POST[\'locale\'];
       if ($locale=="yes"){$locale = "Yes";}
        else {$locale="No";}

$widgets =  $_POST[\'widgets\'];
       if ($widgets=="yes"){$widgets = "Yes";}
        else {$widgets="No";}

$skins =  $_POST[\'skins\'];
       if ($skins=="yes"){$skins = "Yes";}
        else {$skins="No";}

$contact =  $_POST[\'contact\'];
       if ($contact=="yes"){$contact = "Yes";}
        else {$contact="No";}

$press =  $_POST[\'press\'];
       if ($press=="yes"){$press = "Yes";}
        else {$press="No";}

$comm =  $_POST[\'comm\'];
       if ($comm=="yes"){$comm = "Yes";}
        else {$comm="No";}

$page =  $_POST[\'page\'];
       if ($page=="yes"){$page = "Yes";}
        else {$page="No";}

$psd =  $_POST[\'psd\'];
       if ($psd=="yes"){$psd = "Yes";}
        else {$psd="No";}

$format =  $_POST[\'format\'];
       if ($format=="yes"){$format = "Yes";}
        else {$format="No";}

$codes =  $_POST[\'codes\'];
       if ($codes=="yes"){$codes = "Yes";}
        else {$codes="No";}
     $_SESSION[\'error_message\'] = \'You will need to enter the correct captcha value. Please try again!\';
        }

}

?>

1 个回复
SO网友:Tom J Nowell

要解决此问题,您需要通过多个术语而不是单个术语

因此,不是:

$_POST[\'main_cat\']
使用:

array($_POST[\'main_cat\'],$_POST[\'sub_cat\'])
不过,您不需要保存main\\u cat。指定sub\\u cat应该意味着它也在subcat的父术语中

结束

相关推荐

使用wp_Dropdown_USERS和SELECTED()函数?

我有一个自定义的元框,其中有一个下拉列表,通过WP\\u dropdown\\u users显示所有WP用户。它保存了使用post选择的WP用户,以便所有工作正常。但是,是否有办法使其在返回编辑时在下拉列表中显示选定的用户,而不是列表中的第一个用户?我已经将selected()函数用于另一个下拉列表,但我已经动态创建了它。我尝试用wp\\u dropdown\\u用户实现selected(),但没有成功,所以我不确定这是否可行?谢谢