I know there is are a tone of post on this but I can't seems to find them. Here is where I am being stupid:
I have an AJAX resqueting for an id and a integer (will be a margin apply on the DOM later on)
Here is the request:
$(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]);
}
}
);
}
});
});
Here is what I get in the console:
[sebastien20140804130001:45, sebastien20140804130002:30]
0=>[
1=>s
2=>e
3=>b
4=>a
...
See the problem? My response (margin) is a string and my for loop goes through each char. I was expecting something like this instead:
[sebastien20140804130001:45, sebastien20140804130002:30]
sebastien20140804130001=>45
sebastien20140804130002=>30
Just a side note I can't know I much 'key/value' pairs I will receive neither if the IDs will follow each other (numeric order) ([ID:MARGIN_TO_APPLY])
What I am aiming at is to be able to loop through each 'key/value' pair and apply something like this:
$('#'+key).css('margin-left', value);
If you guys needs any other information please ask in the comments I will provide them.
Thanks!
--EDIT--
Here is the server side code:
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;
case 'rn_JRT':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":30,';
}
break;
case 'rn_ACHAT':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_ADM':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_ASC':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_COMP':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_DIRECTION':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_INDUS':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_INFO':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_PROD':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_RED':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_STD':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_TEST':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_VENTE':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_CUSTOME':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_CRP':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":30,';
}
break;
case 'rn_FAB':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_IC':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_MAG':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_PRD':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_SERV':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_VE':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":45,';
}
break;
case 'rn_CAL':
$params = array('DEP'=>str_replace('rn_', '', $dep), 'ID'=>$_POST['sort'][$i]);
$rsSort = $cnSort->SecureExecute($query, $params);
if($rsSort)
{
echo '"'.$_POST['sort'][$i].'":30,';
}
break;
}
$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();
}
}
}
Side note #2: The PHP script is updating the DB with the position of the JQuery sortable items then it echo the element ID and the value for the border.
--EDIT2--
I changed some of the code see code above /--Highlighted this way--/
dataTypeoption in the.ajax()call as 'json' and toecho json_encode()the data on the server side all in one go unless you're very confident about how you build JSON strings. As you iterate through, build it as a php array and then echo the entire thing json_encoded.