views:

50

answers:

3

Hello,

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).

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.

Any ideas?

Here's my code:

function connect () {
    $client_id = $this->uri->segment(3);
    $related_id = $this->uri->segment(5);
    $related_name = $this->uri->segment(6);

    $uri_segments= $this->session->userdata('segments');
    $uri = base_url()."index.php".$uri_segments;

    if (!$this->uri->segment(4)) {
    $this->load->model('get_clients_model');
    $data['records'] = $this->get_clients_model->getAllClients();
    $this->load->view('clients_all_related',$data);
    }

    elseif ($this->uri->segment(4) == "add") {

        $this->load->model('get_clients_model');
        $data['record'] = $this->get_clients_model->getSingleClient($client_id,$related_id,$related_name);

        echo "<script>javascript:alert('".$data['record']."');</script>";
        usleep(2000000);
        redirect($uri); // send back to previous page with updated related contact

    }
}

The relevant part is in the elseif.

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.

Thanks!

+1  A: 

My suggestion would be to have the Javascript handle the redirect instead of the PHP. That way, your PHP script isn't just taking control. Perhaps you are running into a caching issue or something like that with your output.

Anyway, simple. The Javascript outputs the alert, which blocks the redirect from happening until after the user acknowledges it.

echo "<script>javascript:alert('".$data['record']."'); window.location = '".$uri."'</script>";
Ben Dauphinee
Thanks, this solution worked for me! Still can't fgure out why my way didn't work, seems to be doing the same thing.
Robimp
Yours didn't work likely because the output of the echo was being held, so the redirect() happened before your output actually made it to the browser.
Ben Dauphinee
Ah right, that makes sense, thanks for explaining.
Robimp
+1  A: 

You can give a try to that:

header("refresh:20000000;url=".$uri);

As redirect() is a shortcut for a header("location:..."); it should work. Probably you will need the full URL instead of /controller/action.

Isern Palaus
Hi, gave this a try but couldn't get it to work, it just sends it to the controller. Nice try though, would've been a good solution.
Robimp
A: 

:) Man you have to flush(); inorder your code to send. In normal case all output stored then send.

Your method is worst way!

nerkn
Not sure what that means.
Robimp
if you echo then wait say 2 min. Php dont send any thing to browser. You need to explicitly flush the output buffer!
nerkn
Ahhh, yeah I see, will remember that in future. Thanks for your answer!
Robimp