2013-05-14 97 views
0

我正在爲Wordpress工作的前端提交頁面執行多個步驟。If語句之間傳遞表單變量

所以我在頁面上有一個表單,它在幾個階段收集數據並在每個階段後發佈它。

我可以使用GET做到這一點,但我不希望變量在URL中可見,因爲人們很容易編輯他們沒有寫入的博客上的其他帖子。

我該如何去使用這種方法將帖子ID從舞臺傳遞到舞臺? 有多種頁面形式的更好的方法嗎?

正如你可以在下面看到的,我需要在設置步驟和第一步之間傳遞帖子ID,但我不希望它作爲GET在URL中。

UPDATE:

好了,似乎一步確認的階段是在如果else語句完成了我在那裏失去了所有的POST和會話變量,我現在已經改變了它,它顯示帶有隱藏輸入的表單,而不是GET請求的繼續鏈接。更新引擎收錄 - http://pastebin.com/LpHrHwrE

這裏是我使用的代碼:http://pastebin.com/8b4qMNrm

PHP

<?php 
if(isset($_GET['step'])){ 
    $step = $_GET['step']; 

    if ($step == "setup"){ 
    $setup = ""; $step_one =""; 
     if(isset($_POST['submit'])){ 
      $guide_title = trim($_POST['guide_title']); 
      if($guide_title != ""){ 
       $post = array(
        'post_title'  => $guide_title, 
       ); 

       $post_ID = wp_insert_post($post, $wp_error); 
       $stage_complete = true; 
      } else { 
       $message = "Please complete all required fields."; 
      } 
     } else { 
      $guide_title = ""; 
      $stage_complete = false; 
     } 

    } else if($step == "one"){ 
    $setup ="c"; $step_one = ""; 
    if(isset($_POST['submit'])){ 
      $guide_new_title = trim($_POST['guide_new_title']); 

      if($guide_new_title != ""){ 
       $my_post = array(); 
       $my_post['ID'] = $guide_id; 
       $my_post['post_title'] = $guide_new_title; 

       wp_update_post($my_post); 

       $stage_complete = true; 
      } else { 
       $message = "Please complete all required fields."; 
      } 
     } else { 
      $guide_title = ""; 
      $stage_complete = false; 
     } 
    } 
} else { 
    $step = "start"; 
} 

if(empty($message)){ 
    $message = ""; 
} 
?> 

HTML

<?php if($step == "start"){ ?> 
    <form action="<?php the_permalink() ?>?step=setup" method="POST" class="formee"> 
    <input class="button" type="submit" name="submit" value="Go To Post Setup"> 
    </form> 
    <?php } else if($step == "setup"){ ?> 
    <?php echo $message; if($stage_complete == false){ ?> 
    <form action="<?php the_permalink(); ?>?step=setup" method="POST" class="formee"> 
    <label>Guide Title <em class="formee-req">*</em></label> 
    <input type="text" name="guide_title" required="required" value="<?php echo htmlentities($guide_title); ?>"> 
    <input class="button" type="submit" name="submit" value="Setup Post"> 
    </form> 
    <?php } else { $step_one = "c" ?> 
    <p>Post Has Been Setup.</p> 
    <a href="<?php the_permalink(); ?>?step=one" class="button">Continue To Step One &rarr;</a> 
    <?php } ?> 
    <?php } else if($step == "one"){ ?> 
    <?php echo $message; if($stage_complete == false){ ?> 
    <form action="<?php the_permalink(); ?>?step=one" method="POST" class="formee"> 
    <label>Guide Title <em class="formee-req">*</em></label> 
    <input class="button" type="submit" name="submit" value="Rename Post Title"> 
    </form> 
    <?php } else { $step_one = "c" ?> 
    <p>Post Has Been Renamed.</p> 
    <a href="index.php?step=finish" class="button">Finished &rarr;</a> 
    <?php } ?> 
    <?php } ?> 

回答

1

只需要使用您的形式就像一個隱藏字段所以:

<input type="hidden" name="postID" value="<?= $theID ?>" /> 

然後用POST而不是GET作爲表格。

或者,您也可以使用一個會話變量:

session_start(); 
$_SESSION['postID'] = $theID; 

// Access via $_SESSION['postID'] 
+0

我已經使用隱藏域嘗試,但它似乎並不正確,通過空留下後場傳遞給它,當我嘗試使用會話它給出了一個錯誤 - 無法發送會話緩存限制器 - 已發送的標頭。 –

+0

確保'session_start()'在你輸出任何東西之前出現,請在你使用它的每個文件的頂部試試它。 –

+0

如果由於任何原因隱藏字段不是一個選項,$ _SESSION是要走的路。這應該是一個被接受的答案。 – dzumla011