2

I'm doing a check to send alerts if it exceeds the time a task has to be done. Jquery Code:

function session_checking1()
{
    $.post( "./alertaposicionamento", function( data ) {
        if(data == "-1")
        {
           alert('Tem posicionamentos em atraso!');
        }
    });
}
var validateSession1 = setInterval(session_checking1, 10000);

On the alertaposicionamento page I have the following php:

$result1 = mysqli_query($conn, $query2);  
while($row = mysqli_fetch_array($result1))  
      { 
        $teste = $row["codigo"];
        $teste1 = $row["Colaborador"];
        $teste2 = $row["FimTarefa"];
        $teste3 = $row["TipoPeriodicidade"];
        $teste4 = $row["Tempo"];
        $teste5 = $row["Ala"];
        $teste8 = $row["nome"];
      }

$query = "SELECT iduser, hostname FROM raddb.sessoes
WHERE datafim IS NULL AND hostname = '$teste5'";
$result = mysqli_query($conn, $query);  
while($row1 = mysqli_fetch_array($result))  
      { 
        $teste6 = $row1["iduser"];
        $teste7 = $row1["hostname"];
      }         

if($teste4 > $teste3 AND $teste5 == $teste7 AND $teste6 == $_SESSION['usuarioId'])
{
    //expired
    echo "-1";
}
else
{
    //not expired
    echo "1";
}

It works properly and as I intend. I wanted to make an improvement, I wanted to get the value of the $test8 variable, send it by the URL and add the value of that variable to the message I have inside the Jquery alert ('Tem posicionamento em atraso do (and the value of $test8 variable)! ');

Bruno
  • 801
  • 5
  • 11
  • https://stackoverflow.com/questions/439463/how-to-get-get-and-post-variables-with-jquery – Arleigh Hix Jun 27 '19 at 08:29
  • Does your query return more than one row? At the end of the `while` loop you just have the values from the last row, all the other rows are being ignored. If it only returns one row, why do you need a loop?> – Barmar Jun 27 '19 at 08:53

1 Answers1

0

you need to send the value back with your response

header('Content-Type: application/json');
if($teste4 > $teste3 AND $teste5 == $teste7 AND $teste6 == $_SESSION['usuarioId'])
{
    //expired
    echo json_encode(["status"=>"-1","value"=>$teste8]);
}
else
{
    //not expired
     echo json_encode(["status"=>"1","value"=>$teste8]);
}

then change your js to

if(data.status == "-1")
        {
           alert('Tem posicionamento em atraso do '+data.value);
        }
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
  • In this way it sends the value of the variable, but the alert does not appear to the user – Bruno Jun 27 '19 at 08:46