我需要得到一个传递给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