最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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 ment 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 wele.

Thanks!

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 ment 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 wele.

Thanks!

Share Improve this question asked Oct 28, 2010 at 13:02 RobimpRobimp 6986 gold badges14 silver badges29 bronze badges 2
  • alert should freeze the browser, shouldn't need a sleep or anything. doesn't work? – Brandon Frohbieter Commented Oct 28, 2010 at 13:06
  • no just pauses then redirects the page, which wasn't what i expected... – Robimp Commented Oct 28, 2010 at 14:37
Add a ment  | 

3 Answers 3

Reset to default 4

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)

  1. 暂无评论