我需要得到一個傳遞給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);
}
永遠不會呈現JS代碼服務器端的塊。永遠不能。 – moonwave99
永遠不會永遠。 ;-)我在一個應用程序中用服務器端變量填充head.js函數。源變量在會話中,所以傳遞它們是有意義的。但這更像是收集資源,而不是「實際的可執行JS」。 –
不知道你們倆在說什麼:( – user983248