2012-11-14 50 views
0

我需要得到一個傳遞給php腳本的變量$option$option位於名爲「選項」的隱藏字段中。傳遞值到PHP

基本上用戶有一些項目,可以使用像拖動&下拉重新排列。項目存儲在一個數組中,一旦項目重新排列後,數組將被重新排序並保存。

該問題無法通過將get_option($option);更改爲get_option('myoption');解決,我必須從隱藏字段獲取$option

我花了很多時間在網上搜索一個例子,解決方案和教程沒有希望,這對我的頭腦太過分了。

  array_walk($_POST['order'], 'strip_text_menu_editor'); 
      $order_array = $_POST['order']; 
      $current_menu = get_option($option);  
      $new_order = array(); 
      foreach($order_array as $order) { 
       foreach($current_menu as $key => $value) { 
        if($order == $key) { 
         $new_order[] = $value; 
        } 
       } 
      } 
      update_option($option, $new_order); 
      echo '<script type="text/javascript" > 
      jQuery(document).ready(function($) { 
      var i = 0'; 
      echo "jQuery('#sortable li').each(function(){ 
        var name = 'listItem_' + i; 
        i++; 
       }); 
      }); 
      </script>"; 
      die(true); 

jQuery的

  jQuery(document).ready(function(jQuery) { 
       jQuery('#sortable li:odd').addClass('stripe'); 
       jQuery('#sortable').sortable({ 
        handle : '.handle', 
        update: function(event, ui) { 
         jQuery('#sortable li').removeClass('stripe'); 
         jQuery('#sortable li:odd').addClass('stripe'); 
         var order = jQuery('#sortable').sortable('toArray'); 
         var data = { 
          action: 'menu_editor_special_action', 
          order: order, 
         }; 
         jQuery.post(ajaxurl, data, function(response){ 
          jQuery('#response').html('Got this from the server: ' + response); 
         }); 
        } 
       }); 
      }); 

HTML

<ul> 
<li id="listItem_0" class="">Item A</li> 
<li id="listItem_1" class="stripe">Item B</li> 
<li id="listItem_2" class="">Item C</li> 
<li id="listItem_3" class="stripe">Item D</li> 
<li id="listItem_4" class="">Item E</li> 
<li id="listItem_5" class="stripe">Item F</li> 
</ul> 

叫上PHP腳本

function strip_text_menu_editor(&$value, $key) { 
     $value = str_replace('listItem_', '', $value); 
    } 
+0

永遠不會呈現JS代碼服務器端的塊。永遠不能。 – moonwave99

+0

永遠不會永遠。 ;-)我在一個應用程序中用服務器端變量填充head.js函數。源變量在會話中,所以傳遞它們是有意義的。但這更像是收集資源,而不是「實際的可執行JS」。 –

+0

不知道你們倆在說什麼:( – user983248

回答

3

從我所看到的您發佈的jQuery片段中,您需要將'選項'值添加到您的文章中的數據數組中。

var data = { 
    action: 'menu_editor_special_action', 
    order: order, 
    option: $('input[name=option]').val(), //This part is new 
}; 
jQuery.post(ajaxurl, data, function(response){ 
    jQuery('#response').html('Got this from the server: ' + response); 
}); 

當然,我不知道你已經保存什麼樣的數據,在「選項」,我不知道你get_option()等函數用它做知道你需要怎麼送它爲你的服務器端代碼來了解,但這應該讓你開始。

+0

因此,在PHP中從輸入字段獲取值我應該做'$ option = $ _POST ['option'];'? – user983248

+0

是的,假設你的字段被命名爲'option' –