2011-10-20 66 views
1

我是新開發的wordpress插件。我設計了一個搜索表單,但是我不知道在哪裏處理和打印提交的表單數據。 它是在一個wedget基於插件和插件的形式部分代碼是在這裏:Wordpress插件表單問題

function widget($args, $instance) { 
    extract($args); 
     $title = apply_filters('widget_title', $instance['title']); 
       $message = apply_filters('widget_content', $instance['content']); 
     echo $before_widget; 
     //if ($title) 
     // echo $before_title . $title . $after_title; 
     echo '<div class="shk_location_form_holder"> 
        <span class="shk_loc_title">'.$title.' 
        <form mthod="post"> 
        <input type="text" name="shk_inp_search_locations" id="shk_inp_search_locations" /><br> 
        <div style="height:5px"></div> 
        <input type="submit" Value="Search Locations" /> 
        </form></div>'; 
     echo $after_widget; 
       if(isset($_REQUEST['shk_inp_search_locations'])){ 
        add_filter('the_content','handle_content'); 
       } 
    } 
+0

你是表單缺少_action_參數。您可以將它發送到另一個.php文件,將操作留空並在相同的頁面/文件/小部件中處理$ _POST數據或使用[AJAX](http://www.askaboutphp.com/213/php-and- jquery-submit-a-form-without-refreshing-the-page.html)來處理數據。 –

回答

1

在WP插件,你通常有一個空的行動=「」的形式,並在同一個函數處理它(由因爲在WP中輸出任何內容之前加載插件(這就是爲什麼在wp中編寫ajax插件非常簡單的原因),因爲wordpress程序代碼變得非常混亂,所以最好使用OOP編寫插件。所以你可以擁有一切這樣的結構:

function draw_form() { 
    handle_submit(); 
?> 
<div class="shk_location_form_holder"> 
    <span class="shk_loc_title"><?php echo $title; ?></span> 
    <form mthod="post" action=""> 
     <input type="text" name="shk_inp_search_locations" id="shk_inp_search_locations" /><br> 
     <div style="height:5px"></div> 
     <input type="submit" Value="Search Locations" /> 
    </form> 
</div> 
<? 
} 

function handle_submit() { 
    if(isset($_POST['shk_inp_search_locations']) && $_POST['shk_inp_search_locations'] == 'test') { 
     echo 'you may want to end your program here, especially if it\'s ajax!'; 
     exit; 
    } 
}