title: Echo Javascript Alert Before Redirect, usleep(2000000)? - PHP CodeIgniter
body: <p>I'm trying to have a javascript alert, after a database interaction and before a page redirect (back to the same page, but displaying the updated data).</p>
  
  <p>I thought having a pause before the redirect with usleep() might work, but it seems to ignore it. If I comment out the redirect it takes me to the controller page, where the alert pops up.</p>
  
  <p>Any ideas?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>function connect () {
      $client_id = $this-&gt;uri-&gt;segment(3);
      $related_id = $this-&gt;uri-&gt;segment(5);
      $related_name = $this-&gt;uri-&gt;segment(6);
  
      $uri_segments= $this-&gt;session-&gt;userdata('segments');
      $uri = base_url()."index.php".$uri_segments;
  
      if (!$this-&gt;uri-&gt;segment(4)) {
      $this-&gt;load-&gt;model('get_clients_model');
      $data['records'] = $this-&gt;get_clients_model-&gt;getAllClients();
      $this-&gt;load-&gt;view('clients_all_related',$data);
      }
  
      elseif ($this-&gt;uri-&gt;segment(4) == "add") {
  
          $this-&gt;load-&gt;model('get_clients_model');
          $data['record'] = $this-&gt;get_clients_model-&gt;getSingleClient($client_id,$related_id,$related_name);
  
          echo "&lt;script&gt;javascript:alert('".$data['record']."');&lt;/script&gt;";
          usleep(2000000);
          redirect($uri); // send back to previous page with updated related contact
  
      }
  }
  </code></pre>
  
  <p>The relevant part is in the elseif.</p>
  
  <p>As a side note if anyone knows a better way to do this other than the alert, which is just showing a success / fail message, then that would also be welcome. </p>
  
  <p>Thanks!</p>