Reputation: 1540
I have a $.post
function, sending data to a file that procesess many information, and that file can give back different messages on the same call. I am trying to process those messages, identifying them and doing whatever action each one is ment to 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 like "counter?12", so that value
12 goes as $('.totalRegistros span').html(value);
But on the point that it can also echo things like "aviso?There are empty fields", then the whole response goes toghether, at the same time (I'd like to catch each message on the moment of its creation) as a single string: "counter?7aviso?There are empty fields"
How should I send messages from checkMsg.php
to make jQuery know them on real time? And separately: individual messages to be processed on real time.
Here's checkMsg.php
related code with echoes sending this info:
$countMsg = mysqli_query($con,"SELECT * FROM ws_campmsg WHERE 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.";
}
}
Upvotes: 0
Views: 54
Reputation: 31614
I think you're doing a lot of work to parse the data on the JSON end when PHP can simplify this for you. Here's a simple change to your PHP
$data = array();
$data['success'] = false;
$data['aviso'] = array('Your message here', 'Another Message', 'One more message');
echo json_encode($data);
And then this in your jQuery function. Note that checkData
will be recognized as JSON and then parsed automatically
function(checkData) {
if(checkData.success) {
//Successful AJAX call
} else {
$(checkData.aviso).each(function(index, msg) { alert(msg); });
}
}
Putting your responses into JSON is the cleanest and easiest way to get the data you want. Don't reinvent the wheel and it will be easier to work on in the future
Upvotes: 1