如何添加选项以从php数组中选择字段到Contact form 7 Wordpress

时间:2015-02-02 08:27:08

标签: php wordpress contact-form-7

我在wordpress中有一个单页的网站。我需要将来自php数组$ cat_good的信息放在两个选择框中。它在index.php文件中正常工作,如下所示:

<div>
    <select id="flowers_type" class="styled">
        <option value="">--</option>
        <?php 
            foreach ( $cat_good as $key => $value) {
                echo '<option value="' . $key . '">' . $key . '</option>';
            } 
        ?>
    </select>
</div>
<div>
    <select id="flowers_type_item" class="styled">
        <option value="">--</option>
        <?php 
            foreach ( $cat_good as $key => $value) {
                $good = $key;
                foreach ( $value as $key => $value ) {
                    echo '<option class="' . $good . '" value="' . $key . '">' . $value . '</option>'; 
                }
            } 
        ?> 
    </select>
</div>

问题是,如何将这两个选择框与表格7联系起来?

在Dhanuka Nuwan的帮助下,我现在在function.php中有代码,这有助于我添加选择器以联系表单7.

function flowers_type(){<!-- here is my code for $cat_good -->
$output .= "<div><select name='flowers_type' id='flowers_1' class='styled'><option value='0'>--</option>";      
    foreach ( $cat_good as $key => $value) {
        $output .= "<option value='$key'>$key</option>";         
     }
$output .= "</select></div>";
$output .= "<div><select name='flowers_type_item' id='flowers_2' class='styled'><option value='0'>--</option>";     
    foreach ( $cat_good as $key => $value) {
        $good = $key;
        foreach ( $value as $key => $value ) {
            $output .= "<option class='$good' value='$key'>$value</option>";
        }
     }
     $output .= "</select></div>";
     return $output;}

但我还需要第二个选择器来自第一个选择器。我试图通过帮助https://github.com/tuupola/jquery_chained来做到这一点。在我的js文件中,我有:

$("#flowers_2").chained("#flowers_1");

不幸的是,它没有用。

1 个答案:

答案 0 :(得分:4)

您可以轻松地使用wpcf7_add_shortcode为表单7添加短代码。这是你的代码。

function flowers_type(){
   $output = "<select name='flowers_type' id='flowers_type' onchange='document.getElementById(\"flowers_type\").value=this.value;'><option value="">--</option>";
   foreach ( $cat_good as $key => $value) {
            $output .= "<option value='$key'> $key </option>";
        } 

   $output .= "</select>";
return $output;
}

wpcf7_add_shortcode('flowers_type', 'flowers_type', true);

现在您可以在联系表单7表单中使用[flowers_type]短代码。请注意,此代码未经过测试。使用前备份您的文件。对你的另一个做同样的事。

玩得开心。 :)