Question: I am passing PHP value to AJAX but I am getting an error that variable is not defined.

```
$value_r is not defined

```

When the user selects the checkbox then the value of $value\_r will auto display.

**AJAX**

```
$(document).ready(function () {
        $("input[name=value_r]").click(function(){
            $.ajax({
                type: "POST",
                url: "includes/compare.php", // 
               data: {id: <?php echo '$value_r'?>},
                success: function(msg){
                    alert(msg);
                },
                error: function(){
                    alert("failure");
                }
            });
        });
    });

```

**index.php**

```
<input  type="checkbox" value="" name="value_r" id="value_r"/>
<label for=""><?php echo $value_r?></label>
    <style type="text/css">
        input[type=checkbox] + label{
    display: none;
}
input[type=checkbox]:checked + label{
    display: inline-block;
}
</style>

```

Answer: 
The problem is that your `readServerStatus()` call is async and therefore will print the value when it's emitting a value. But the rest of your code doesn't wait for it, so your second `console.log` prints an empty object.

But there's a huge problem with your code, your generating tons of subscriptions inside your `while` loop without completing them, which will lead to a memory leak.

I would suggest to use the `repeatWhile` operator with `takeUntil` of RxJs for such a task.

Here's an example:

```js
let mockResponse = { serverBusy: 1, terminate: 0 };
const request = () => {
  return Rx.Observable.of(mockResponse).delay(500); // emulate api call
};

request()
  // repeat the request while isBusy is true
  .repeatWhen(notifications => {
    return notifications.takeWhile(() => {
      return !!mockResponse.serverBusy;
    });
  })
  .subscribe(res => {
    console.log(res);
  });

setTimeout(() => {
   mockResponse.serverBusy = 0;
}, 2000);
```

```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.4.3/Rx.min.js"></script>
```

Here's another example using `switchMap` to read the data from the response and decide if you need to retry, if not emit the response.

```js
let mockResponse = { serverBusy: 1, terminate: 0 };
const apiCall = () => {
  return Rx.Observable.of(mockResponse).delay(500); // emulate api call
}

function makeRequest$() {
  let request$ = apiCall()
    .switchMap(res => {
      console.log('is busy:', !!res.serverBusy);
      return !!res.serverBusy ? request$ : Rx.Observable.of(res);
    });
  return request$;
}

makeRequest$().subscribe(res => {
  console.log(res);
});

setTimeout(() => {
   mockResponse.serverBusy = 0;
}, 2000);
```

```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.4.3/Rx.min.js"></script>
```
it's happening because the console print outside the subscription will not wait for the new value assignment. It will be printed before new value arrives and assings `this.surveyStatus = JSON.parse(JSON.stringify(res));`. So while you are subscribing, if you wanted to do anything on value changes, put whole thing inside the subscription.