decoding jquery json data in php
Posted
by Mac Taylor
on Stack Overflow
See other posts from Stack Overflow
or by Mac Taylor
Published on 2010-04-26T15:10:44Z
Indexed on
2010/04/26
15:13 UTC
Read the original article
Hit count: 177
hey guys recenlty i made a script to move objects and save the orders
now i have a little to do to finish this script
everything works fine , just one question :
how can i save not numeric data in my json script
this is my script :
function updateWidgetData(){
var items=[];
$('.widget-title').each(function(){
var weightId=$(this).attr('id');
$('.column').each(function(){
var columnId=$(this).attr('id');
$('.widget', this).each(function(i){
var collapsed=0;
if($(this).find('.widget-inside').css('display')=="none")
collapsed=1;
//Create Item object for current panel
var item={
id: $(this).attr('id'),
collapsed: collapsed,
order : i,
column: columnId,
weight: weightId
};
//Push item object into items array
items.push(item);
});
});
});
//Assign items array to sortorder JSON variable
var sortorder={ items: items };
//Pass sortorder variable to server using ajax to save state
$.post('updatePanels.php', 'data='+$.toJSON(sortorder), function(response){
if(response=="success")
$("#console").html('<div class="success">Saved</div>').hide().fadeIn(1000);
setTimeout(function(){
$('#console').fadeOut(1000);
}, 2000);
});
}
and this is my php script :
$data=json_decode($_POST["data"]);
foreach($data->items as $item)
{
//Extract column number for panel
$col_id=preg_replace('/[^\d\s]/', '', $item->column);
//Extract id of the panel
$widget_id=preg_replace('/[^\d\s]/', '', $item->id);
$sql="UPDATE widgets SET column_id='$col_id', sort_no='".$item->order."', collapsed='".$item->collapsed."' WHERE id='".$widget_id."'";
mysql_query($sql) or die('Error updating widget DB');
}
echo "success";
everything works fine till i use numeric value for columns' id but i need non numeric values forexample
id='columnr'
i want to extract r
but i cant get it right
any help plz !?
© Stack Overflow or respective owner