2011-02-13 312 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 { 
    ... 
} 

如果有任何在所有的「狀態」字段的形式可以是在橫跨形式的所有版本通用,然後對其進行處理的條件塊之外。