U
U
Uniq2016-08-19 18:36:57
PHP
Uniq, 2016-08-19 18:36:57

AJAX + PHP gradual output?

I can't figure out how to display multiple results after sending $_Post[] request via ajax using foreach.

js only gets the first return $info; the rest is not processed.

MY CODE:

JS - OUTPUT AND SEND

$(function(){
              $.ajax({
                type: "POST",
                url: "/proxy",
                data:'xxx='+JSON.stringify(post),
                dataType: 'JSON',                
                success: function(html){
                   .info.html('<div class="alert">'+html+'</div>'); }
});
      });

PHP - (POST request handler)
if(isset($_POST['xxx'])){
$like = $_POST['xxx'];
$result = $op->start($like); 
echo json_encode($result);
 exit();

PHP - PROCESSING AND SELECTING THE RESULT
public function start ($message) {
foreach ($user as $user){ 
$status = $op->post($message,$user,$us);
if($status == false){ 
$info = "error"; 
}else{
$info = "ok";
}
}
return $info;    
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Koryukov, 2016-08-19
@MadridianFox

Rather, not the first $info, but the last one, because you are overwriting a variable in a loop.
Perhaps you wanted to write

public function start ($message) {
    $info=[];
    foreach ($user as $user){ 
        $status = $op->post($message,$user,$us);
        if($status == false){ 
            $info[] = "error"; 
        }else{
            $info[] = "ok";
        }
    }
    return $info;    
}

A
Alexander Sharihin, 2016-08-19
@Pinsky

something is wrong with you and it’s not clear what you did.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question