Bor
Bor

Reputation: 793

Passing value to JS with php return?

I have a php method that should return some values as JSON :

function pass_value() {
 .... 
 $output[] = 'some value';
 ...

 if() { 
  //JS get it right
  echo json_encode(array('response' => $output));
  return;
 }
 ...
 //eventually another echo 

}

Where's the problem to do it like this ?

function pass_value() {
 .... 
 $output[] = 'some value';
 ...

 //js doesn't get it ?
 return json_encode(array('response' => $output)); 
}

I want to terminate the other part of the method after it passes the array, but seems that JS doesn't get the JSON when it's not echoed.

Upvotes: 0

Views: 40

Answers (1)

Bart Friederichs
Bart Friederichs

Reputation: 33521

That's because if you do not echo, the server never sends it to the client and the JavaScript never sees it. Remember, PHP is server-side, JavaScript is client-side.

You can of course do this with the last function:

echo pass_value();

Upvotes: 1

Related Questions