There are many posts on this topic, but I am having trouble finding the specific information I need. Here is where I am struggling:
I have an AJAX request for an ID and an integer (which will be used as a margin to apply on the DOM later).
This is the request I am making:
$(function(){
$("#divAccordion").accordion({
//Accordion options here, not relevant
}).sortable({
//Other sortable options here, not relevant
//This is the important part:
update:
function(event, ui){
var data = $(this).sortable('toArray');
$.ajax({
url:"prc.php",
type:"POST",
dataType:'JSON',/*--Added this also--*/
data:{sort:data},
contentType : "application/x-www-form-urlencoded;charset=UTF-8"
}).done(
function(margin){
console.log(margin);//This log [sebastien20140804130001:45, sebastien20140804130002:30]
for(key in margin){
console.log(key+"=>"+margin[key]);
}
}
);
}
});
});
When I check the console, this is what I see:
[sebastien20140804130001:45, sebastien20140804130002:30]
0=>[
1=>s
2=>e
3=>b
4=>a
...
Do you see the issue? My response (margin) is being returned as a string, causing my loop to iterate through each character instead of key/value pairs as expected.
Instead, I would like the output to look something like this:
[sebastien20140804130001:45, sebastien20140804130002:30]
sebastien20140804130001=>45
sebastien20140804130002=>30
One challenge I face is not knowing how many 'key/value' pairs I will receive or if the IDs will be in numeric order ([ID:MARGIN_TO_APPLY]).
My goal is to loop through each 'key/value' pair and apply something like this:
$('#'+key).css('margin-left', value);
If you need any additional information, please let me know in the comments section.
Thank you!
--EDIT--
Below is the server-side code that handles the request:
echo '{';/*--was [--*/
sortRN('rn_GLOBAL', 0);
echo '}';/*was ]--*/
function sortRN($dep, $i)
{
if(isset($_POST['sort'][$i]) && $_POST['sort'][$i] != '')
{
$cnSort = new cConnexion('***', '***', '***', '***');
if($cnSort->DBConnexion())
{
$query = "UPDATE ***.reunion SET rn_DEP = :DEP WHERE REPLACE(REPLACE(REPLACE(CONCAT('rn_', rn_SAMAORG, rn_DTSTART), ' ', ''), ':', ''), '-', '') = :ID";
while(isset($_POST['sort'][$i]) && preg_match('#^.+[0-9]{4}$#', $_POST['sort'][$i]))
{
if($i > 0)
{
echo ',';/*--Added this so there wouldn't always be a coma at the end--*/
}
switch($dep)
{
case 'rn_GLOBAL':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '{"'.$_POST['sort'][$i].'":15}';/*--Changed every line like this to add {} and also removed the coma at the end--*/
}
break;
/* More cases follow... */
}
$i++;
}
$cnSort->DBDeconnexion();
if(isset($_POST['sort'][$i]) && $_POST['sort'][$i] != '')
{
$ii = $i + 1;
sortRN($_POST['sort'][$i], $ii);
}
}
else
{
echo $cnSort->m_log->getMessageFR();
}
}
}
A side note: The PHP script updates the database with the position of the jQuery sortable items and then echoes the element ID along with its corresponding margin value.
--EDIT2--
I've made some changes to the code, highlighted above.