根据无线电选择隐藏和显示字段

时间:2011-02-13 23:25:10

标签: php javascript jquery wordpress

我有以下表格:

<fieldset id="upload-form">                     
                        <ul>
                            <li>
                                <div class="radio-list clearfix">
                                <ul>
                                    <li><label for="r1"><input type="radio" name="category" id="r1" value="34" checked="checked" /> Discussion</label></li>
                                    <li><label for="r2"><input type="radio" name="category" id="r2" value="35" /> Question</label></li>
                                    <li><label for="r3"><input type="radio" name="category" id="r3" value="36" /> Code</label></li>
                                    <li><label for="r4"><input type="radio" name="category" id="r4" value="37" /> Link</label></li>
                                </ul>
                                </div>
                            </li>
                            <li>
                                <label for="title">Title <span>required &amp; make it descriptive</span></label>
                                <input id="title" type="text" name="title" />
                            </li>
                            <li>
                                <label for="url">URL <span>don't forget the http://</span></label>
                                <input id="url" type="text" name="url" placeholder="http://" />
                            </li>
                            <li>
                                <label for="topic">Discussion topic</label>
                                <textarea id="topic" name="topic"></textarea>
                            </li>
                            <li>
                                <label for="question">Your question <span>help text</span></label>
                                <textarea id="question" name="question"></textarea>
                            </li>
                            <li>
                                <label for="description">Description <span>help text</span></label>
                                <textarea id="description" name="description"></textarea>
                            </li>
                            <li>
                                <label for="code">Code</label>
                                <textarea id="code" name="code"></textarea>
                            </li>
                            <li>
                                <label for="tags">Tags <span>separate multiple tags with commas</span></label>
                                <textarea id="tags" name="tags"></textarea>
                            </li>
                            <li>
                                <label class="public" for="public"><input id="public" type="checkbox" name="public" /> Make this post public? <span>( non-members will be able to view this post )</span></label>
                            </li>
                            <li class="clearfix">
                                <input class="submit" type="submit" name="" value="Post" />
                                <input class="cancel" type="button" value="Cancel" onclick="window.location.href='<?php bloginfo('url'); ?>/posts'" />
                            </li>
                        </ul>
                    </fieldset>

当用户选择表单顶部的一个单选按钮时,它将隐藏或显示与该类别相关的某些字段。因此,例如对于链接类别,使用的唯一字段将是标题,URL和描述。

我计划使用jQuery隐藏和显示字段,但服务器端代码呢?好像用户开始填写字段但后来决定更改类别我不希望他们发布错误的字段等。我将如何做到这一点?

我正在使用WordPress,表单使用PHP编码。下面是一些示例代码,说明表单如何获取帖子信息并将其保存到数据库中。感谢。

<?php

    /* Template Name: New Post */

    if(isset($_POST['new_post']) == '1')
    {
        $post_title = $_POST['post_title'];
        $post_category = $_POST['cat'];
        $post_content = $_POST['post_content'];
        $post_timelimit = $_POST['timelimit'];

        $new_post = array(
              'ID' => '',
              'post_author' => $user->ID, 
              'post_content' => $post_content, 
              'post_title' => $post_title,
              'post_status' => 'publish',
              'tax_input' => array('timelimit' => $post_timelimit)
            );

            $post_id = wp_insert_post($new_post);
            // tags have to be added seperate? :/
            wp_set_post_tags($post_id, $_POST['post_tags']);

            // This will redirect you to the newly created post
            $post = get_post($post_id);
            wp_redirect($post->guid);
    }

    get_header();

?>

1 个答案:

答案 0 :(得分:2)

你必须让你的PHP以改变那种形式的单选按钮的值为条件。

if (form is in state 1) {
   process state 1 form fields
} else if (form is in states 2 and 3 or 12) {
   etc...
} else {
   ...
}

如果表单的所有“状态”中的任何字段在表单的所有版本中都是通用的,则在条件块之外处理它们。