title: How to submit value and show add/remove button?
body: <p>I have an array:</p>
  
  <pre><code>$userPosts
  </code></pre>
  
  <p>Then I have a form and I check if current page id is there, if so show remove button otherwise display an add button</p>
  
  <pre><code>&lt;form id="savedPosts" action="" method="POST" class="" autocomplete="off"&gt;
      &lt;input type="hidden" name="save_post" value="&lt;?php echo $postId; ?&gt;"&gt; 
      if (in_array($postId, $userPosts)) {?&gt;
              &lt;button id="remove_post" name="remove_post" type="submit" class="save_post" data-toggle="tooltip" data-placement="top" title="aggiungilo alla tua box"&gt;Remove&lt;/button&gt;
          &lt;?php } else {   
          ?&gt;
              &lt;button id="save_post" name="save_post" type="submit" class="save_post" data-toggle="tooltip" data-placement="top" title="aggiungilo alla tua box"&gt;Add&lt;/button&gt;
      &lt;?php  }
  &lt;/form&gt;
  </code></pre>
  
  <p>Then I created the send bit:</p>
  
  <pre><code>if ($_SERVER['REQUEST_METHOD'] == "POST") {
      if (isset($_POST['save_post'])) {
          $userPosts = implode(',', $userPosts);
          $userPosts = trim($userPosts);
          $userPosts = ltrim($userPosts, ",");    
          update_user_meta( $user_id, 'save_post', $userPosts );
      }
      if (isset($_POST['remove_post'])) {
          $userPosts = explode(',', $userPosts);
          if (($key = array_search($postId, $userPosts)) !== false) {
              unset($userPosts[$key]);
          }
          $userPosts = implode(',', $userPosts);
          $userPosts = trim($userPosts);
          $userPosts = ltrim($userPosts, ",");
  
          update_user_meta( $user_id, 'save_post', $userPosts );
      }
  }
  </code></pre>
  
  <p>That works, however when I click <code>add</code> or <code>remove</code> if the <code>id</code> is in the <code>array</code>, the page sends the correct value but it shows me the old button. I need to manually refresh to display the correct button.</p>
  
  <p>Looks like some sort of caching. I tried to use </p>
  
  <pre><code>ob_start();
  $myurl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
  header('Location: '.$myurl);
  ob_end_flush(); 
  </code></pre>
  
  <p>But I still see the old buttons, not sure if I placed that bit in the wrong position as per the following or it is simply wrong how I used it</p>
  
  <pre><code>if ($_SERVER['REQUEST_METHOD'] == "POST") {
      if (isset($_POST['save_post'])) {
          $userPosts = implode(',', $userPosts);
          $userPosts = trim($userPosts);
          $userPosts = ltrim($userPosts, ",");    
          update_user_meta( $user_id, 'save_post', $userPosts );
          ob_start();
          $myurl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
          header('Location: '.$myurl);
          ob_end_flush();
      }
      if (isset($_POST['remove_post'])) {
          $userPosts = explode(',', $userPosts);
          if (($key = array_search($postId, $userPosts)) !== false) {
              unset($userPosts[$key]);
          }
          $userPosts = implode(',', $userPosts);
          $userPosts = trim($userPosts);
          $userPosts = ltrim($userPosts, ",");
  
          update_user_meta( $user_id, 'save_post', $userPosts );
          ob_start();
          $myurl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
          header('Location: '.$myurl);
          ob_end_flush();
      }
  }
  </code></pre>
  
  <p><strong>WHOLE CODE</strong></p>
  
  <pre><code>&lt;?php 
      global $bodyClass;
      $bodyClass = "single";
  
  /*
      Template Name: External
  */
  
      include ('header_single_b.php'); 
  ?&gt;
  
  &lt;main role="main" class="flex-shrink-0"&gt;
  
      &lt;?php
          $postId = $_GET['postId'];
          $allposts = '';
          $user_id = get_current_user_id();
          $userPosts= get_user_meta( $user_id, 'save_post', TRUE );
          $userPosts = explode(',', $userPosts);
          $userPosts = array_values(array_unique($userPosts));
          $response = wp_remote_get('https://example.com/wp-json/wp/v2/posts?include='.$postId);
          if ( is_wp_error( $response ) ) {
              return;
          }
          $posts = json_decode( wp_remote_retrieve_body( $response ) );
          if ( empty( $posts ) ) {
              return;
          }
          // If there are posts.
          if ( ! empty( $posts ) ) {
              // For each post.
              foreach ( $posts as $post ) {
                  $allposts .= '&lt;a href="' . esc_url( $post-&gt;link ) . '" target=\"_blank\"&gt;' . esc_html( $post-&gt;title-&gt;rendered ) . '&lt;/a&gt;  ' . esc_html( $fordate ) . '&lt;br /&gt;'.$post-&gt;content-&gt;rendered;
              ?&gt;
              &lt;div class="container margin-top-80 margin-bottom-80"&gt;
                  &lt;div class="row"&gt;
                      &lt;div class="col-8"&gt;
                          &lt;form id="savedPosts" action="" method="POST" class="" autocomplete="off"&gt;
                              &lt;input type="hidden" name="save_post" value="&lt;?php echo $postId; ?&gt;"&gt; 
                              &lt;?php
                                  if (in_array($postId, $userPosts)) {?&gt;
                                          &lt;button disabled="disabled" type="submit" class="save_post disabled btn btn-outline-dark" data-toggle="tooltip" data-placement="top" title="già nei tuoi favoriti"&gt;Già nella box&lt;/button&gt;
                                          &lt;button id="remove_post" name="remove_post" type="submit" class="save_post" data-toggle="tooltip" data-placement="top" title="aggiungilo alla tua box"&gt;Rimuovi dalla box&lt;/button&gt;
                                      &lt;?php } else {   
                                      ?&gt;
                                          &lt;button id="save_post" name="save_post" type="submit" class="save_post" data-toggle="tooltip" data-placement="top" title="aggiungilo alla tua box"&gt;Salva nella box&lt;/button&gt;
                                  &lt;?php  }
                              ?&gt;
                          &lt;/form&gt;
                          &lt;hr&gt;
                          &lt;h2&gt;&lt;?php echo esc_html( $post-&gt;title-&gt;rendered ); ?&gt;&lt;/h2&gt;
                          &lt;p&gt;&lt;?php echo $post-&gt;content-&gt;rendered; ?&gt;&lt;/p&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;?php }
          }
          if (!in_array($postId, $userPosts)){
              array_push($userPosts,$postId);
          }   
          if ($_SERVER['REQUEST_METHOD'] == "POST") {
              if (isset($_POST['save_post'])) {
                  $userPosts = implode(',', $userPosts);
                  $userPosts = trim($userPosts);
                  $userPosts = ltrim($userPosts, ",");    
                  update_user_meta( $user_id, 'save_post', $userPosts );
              }
              if (isset($_POST['remove_post'])) {
                  $userPosts = explode(',', $userPosts);
                  if (($key = array_search($postId, $userPosts)) !== false) {
                      unset($userPosts[$key]);
                  }
                  $userPosts = implode(',', $userPosts);
                  $userPosts = trim($userPosts);
                  $userPosts = ltrim($userPosts, ",");
                  update_user_meta( $user_id, 'save_post', $userPosts );
              }
          }
      ?&gt;
  &lt;/main&gt;
  &lt;?php include ('footer_single_compare.php'); ?&gt;
  </code></pre>