0

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.

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

So my original solution appears to be fine ? Echoing sequenced by return statement ?
@Borislav yes. But you can try that out for yourself and find out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.