2011-02-13 7 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

+0

샘플 코드입니다. 실제 폼 코드는 아닙니다. – Cameron

답변

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

모든 "상태"에있는 필드의 양식이 될 수있는 경우

다음 조건 블록 외부에서 처리 양식의 모든 버전에서 공통이다.

+0

그러면 어떻게 PHP로 상태를 확인할 수 있습니까? 라디오의 값 (주정부 상태)은 원래 질문에 표시됩니다. 감사. – Cameron

+1

'if ($ _POST [ 'category'] == 34) {... 카테고리 34 자료}'. 이것은 절대적으로 지하실 수준의 PHP 폼 핸들링입니다. –

관련 문제