php - Process separately different ajax responses to $post success -
i have $.post
function, sending data file procesess many information, , file can give different messages on same call. trying process messages, identifying them , doing whatever action each 1 ment do:
$.post('../actions/checkmsg.php',{ca_id:idcampaign}).success( function(checkdata){ aviso=0; if (checkdata.indexof("counter") >= 0){ var value = checkdata.substr(checkdata.indexof("?") + 1); $('.totalregistros span').html(value); progress.update(n=1); } else if (checkdata.indexof("aviso") >= 0){ var value = checkdata.substr(checkdata.indexof("?") + 1); aviso++; $('.totalavisos span').html(aviso); } }); }
the checkmsg.php
file can echo messages "counter?12", value
12 goes $('.totalregistros span').html(value);
but on point can echo things "aviso?there empty fields", whole response goes toghether, @ same time (i'd catch each message on moment of creation) single string: "counter?7aviso?there empty fields"
how should send messages checkmsg.php
make jquery know them on real time? , separately: individual messages processed on real time.
here's checkmsg.php
related code echoes sending info:
$countmsg = mysqli_query($con,"select * ws_campmsg cm_fk_ca_id='$ca_id'"); $totalregistros=mysqli_num_rows($countmsg); echo "counter?".$totalregistros; //inspecciÓn de variables $vars1=0; $vars2=0; $vars3=0; $hasvar1=0; $hasvar2=0; $hasvar3=0; while($registromsgs = mysqli_fetch_array($countmsg)){ if ($registromsgs['cm_var1']!==""){ $vars1++; $hasvar1=1; } if ($registromsgs['cm_var2']!==""){ $vars2++; $hasvar2=1; } if ($registromsgs['cm_var3']!==""){ $vars3++; $hasvar3=1; } } if ($hasvar1==1 && $hasvar2==1 && $vars1 != $vars2){ if(($hasvar3==1) && ($vars3 != $vars2 || $vars3 != $vars1)){ echo "aviso?algunos de los ".$totalregistros." no tienen variables asignadas: hay ".$vars1." variables de tipo 1 asignadas, ".$vars2." variables de tipo 2 asignadas y ".$vars3." variables de tipo 3 asignadas."; } else{ echo "aviso?algunos de los ".$totalregistros." no tienen variables asignadas: hay ".$vars1." variables de tipo 1 asignadas, y ".$vars2." variables de tipo 2 asignadas."; } }
i think you're doing lot of work parse data on json end when php can simplify you. here's simple change php
$data = array(); $data['success'] = false; $data['aviso'] = array('your message here', 'another message', 'one more message'); echo json_encode($data);
and in jquery function. note checkdata
recognized json , parsed automatically
function(checkdata) { if(checkdata.success) { //successful ajax call } else { $(checkdata.aviso).each(function(index, msg) { alert(msg); }); } }
putting responses json cleanest , easiest way data want. don't reinvent wheel , easier work on in future
Comments
Post a Comment