$ .post return 2 values

2

I have following code:

 <script>

    $.post('http://localhost/app/user.php', {acao: acao}, function(retorna){ 

     $("#demo").html(retorna);

     if(retorna == "sucesso") {faça x}

    });

 </script>

However, in user.php it returns several echo of conditions if . So if I have in PHP, for example, echo "sucesso"; and other echo "sucesso"; it returns successes success .

I would like to know how I can get each of this return separately?

    
asked by anonymous 11.06.2016 / 01:52

1 answer

3

To retrieve each information from this return, use the json_encode function and return only one echo for your Javascript .

Example:

$resposta['status'] = 'success';
$resposta['line'] = 1;

echo json_encode($resposta);

After running this echo has a format JSON :

{"status":"success","line":1}

Returning from your javascript do:

jQuery.post ()

 <script>

    $.post('http://localhost/app/user.php', {acao: acao}, 
    function(retorna)
    { 

        if (retorna.status == 'success')
        {
           //faça alguma coisa 
        }

        if (retorna.line == 1)
        {
            //faça mais alguma coisa
        }
    },'json');

 </script>

That is, it returns a given in the JSON format for that your Javascript , work with the feedback information. You can also receive a list of information .

References:

11.06.2016 / 02:01