multiple echo json_encode from php script to jquery

I would like to create something like “steps processing” in php and jquery (ajax). I have so far that code:

var steps = ["step1", "step2", "step3"];

$.ajax({                                             
    method: "POST",                                  
    url: "test_steps.php",       
    data: {steps: steps},                            
    dataType: "json",                                
    success: function(response) {                    
        console.log(response);
    },
});

and in php something like:

$steps = $_POST['steps'];                   
                                        
if (isset($steps['step1'])) {               
    if ($test->do_some_function1()) {       
        echo json_encode("step1 - success");
    }                                       
    else {                                  
        echo json_encode("step1 - fail");   
    }                                       
}                                           
                                        
if (isset($steps['step2'])) {               
    if ($test->do_some_function2()) {       
        echo json_encode("step2 - success");
    }                                       
    else {                                  
        echo json_encode("step2 - fail");   
    }                                       
}                                           
                                        
if (isset($steps['step3'])) {               
    if ($test->do_some_function3()) {       
        echo json_encode("step3 - success");
    }                                       
    else {                                  
        echo json_encode("step3 - fail");   
    }                                       
}                                           
                                        
exit;                                       

In final solution I want to have it in some kid of modal, all steps with statuses. But at the beginning I would like to wait for them and get all of them in console log, one by one. How to achieve that? Let’s say:

step1 - success
step2 - success
step3 - fail

As for now I get an error and all messages from PHP in error message. So it seems that I am not able to wait for all echo json_encode calls.

Thanks in advance

Read more here: Source link