开发者

Echo Javascript Alert Before Redirect, usleep(2000000)? - PHP CodeIgniter

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->m开发者_如何转开发odel('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!


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>";


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.


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

Your method is worst way!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜