2012-11-14 47 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' –