2011-02-13 318 views
0

我有以下形式:隐藏和显示领域

<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(); 

?> 
+0

这个php需要更新表单,没有任何输入被查看。 $ _POST应与表单输入的名称匹配。 – Jacob 2011-02-13 23:31:07

+0

这是示例代码,我从来没有说过它是实际的形式代码! – Cameron 2011-02-14 00:19:16

回答

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 { 
    ... 
} 

如果有任何在所有的“状态”字段的形式可以是在横跨形式的所有版本通用,然后对其进行处理的条件块之外。