Welcome Guest, Not a member yet? Register   Sign In
Redirecting and setting flash message,..
#1

[eluser]quest13[/eluser]
I have problem in redirecting to any page.I am getting "Cannot modify header information"

I am not able to set the flash message either.I think something is terribly wrong with my configuration. Can anyone help me in this regard ?


This is how I set my config.php.


$config['sess_cookie_name'] = 'tbl_sessions';
$config['sess_expiration'] = 7200;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'tbl_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = FALSE;
$config['sess_time_to_update'] = 300;
#2

[eluser]Dam1an[/eluser]
You get this error once you've sent some output to the screen, and stry to d something like a redirect.
Have you intentionally output something before that point? If so, thats the problem
If not, then you probably output some whitespace somewhere, most likely a new lines after a closing tag (this is why CI doesn't do closing tags)
#3

[eluser]quest13[/eluser]
Thank you very much for your immediate response.I would like to elaborate little more to explain my case further.

I have a function like the one below.It is a simple contact us page.After inserting the data I want to redirect to the main page and display the message as mentioned.

Controller:


function Addcontact(){

$this->form_validation->set_rules('First_and_Last_Name','Name','trim|required|');
$this->form_validation->set_rules('Phone','Phone','trim|required|alpha_dash');
$this->form_validation->set_rules('Email','Email','trim|required|valid_email');

$this->form_validation->set_rules('Message','Message','trim|required|');

if ($this->form_validation->run() == FALSE){

$this->index();// Takes to the home page

}
else {

$data['success']=$this->contactus_model->Insertcontact();

if($data['success']>0){
$this->session->set_flashdata('message', 'Thank you! Your Message Posted Successfully');

redirect('/contactus/index');


}else {

$this->session->set_flashdata('error', 'Error loading form !');

redirect('/contactus/index');

}

}


and In my view page, I did like this :


<span>&lt;?php if($this->session->flashdata('message')) {
echo '<div class="message"> ';
echo '<p>'.$this->session->flashdata('message').'</p>';

$this->session->keep_flashdata('message');

echo'</div>';
}?&gt;</span>

<br />

<span>&lt;?php if($this->session->flashdata('error')) {
echo '<div class="error"> ';
echo '<p>'.$this->session->flashdata('error').'</p>';

$this->session->keep_flashdata('error');

echo'</div>';
}

No message has been displayed. I am getting the following error message

Message: Cannot modify header information - headers already sent by("c:/.....")

Filename: libraries/Session.php

Line Number: 662

Line no 662 is basically about storing cookie information. So I got a doubt that there is something wrong with my cofig settings.

Another question; I don't want to use session at my contact page.In that case, Is it possible to use set_flashdata ?

Thanks
#4

[eluser]Dam1an[/eluser]
What are the chances of that, you truncate the one line I actually needed lol
When you get the error saying
Quote:Message: Cannot modify header information - headers already sent by(“c:/.....”)
It will tell you the file and line number you send output, that is cauing the problem

Line 662 in the session class is just where it noticed the error

Also, can you use [ code ] tags in the future when posting large chunks of code
#5

[eluser]quest13[/eluser]
I am really sorry to give you an insufficient data.But you did a great help by giving me the most important clue ! You were 100% right ! It was due to the white spaces ! I removed the close tag of php and closed with the comments, removed all white spaces, and it is done.

I will definitely place the code in the tags next time as you suggested.

Thanks a lot once again.
#6

[eluser]quest13[/eluser]
I am able to redirect and display the message now. But I still face one problem.
Even after removing "$this->session->keep_flashdata('message')" , the message remain on the other page where no edit or delete carried out. I want to just display as long as I remain in that page, I don't want to carry this to any other page.How to achieve this ?

Is there any unset_flashdata exists in CI ? I tried,but couldn't find one.

Thanks in advance
#7

[eluser]jedd[/eluser]
Flashdata will survive just one page hop.

If you're seeing it where you don't want it, then you're either writing it where you think you are not, or you need to put some logic around the call to control when you view it (it sounds like you're seeing it on a page that should never display it - so I'm not sure why you're trying to display it there anyway - so I'm assuming I have misinterpreted you).
#8

[eluser]quest13[/eluser]
I solved that issue. I gave the same 'message' value to flashdata in both the pages ! so by changing into different values, it is solved.

But I am not able to see the message unless I refresh or reload the page manually. Ofcourse, this can be solved by adding 'refresh' in the third parameter of 'redirect', but when I load a view directly, how do I refresh ? Any idea ?

Thanks to both of you for the valuable comments.It really made sense.
#9

[eluser]jedd[/eluser]
[quote author="quest13" date="1246120822"]
But I am not able to see the message unless I refresh or reload the page manually.[/quote]

Yes, flashdata is available on the loading of the next page. I think you're getting the hang of it now.

If you want to see data on the current page, just store and access a variable ($this->data[...] seems like a good place). Though this may indicate your logic is sub-optimal if you need to generate global variables.

If you want to see data on other pages, use session data.
#10

[eluser]quest13[/eluser]
Thanks for the response.

Following is the code.After setting the data, I just call the 'view' and display it.The message flashes only after I refresh.But when I redirect this same message on some other controller, it works without refreshing. On this particular place, I have to load the view and cannot redirect.

Hope I made my point clear.

$data['result']=$this->Addproperty_model->Addproduct();

$this->session->set_flashdata('add_message', 'Data successfully added !');
$this->template->load('/admin/template', '/admin/Addform', $data,'');

Thanks




Theme © iAndrew 2016 - Forum software by © MyBB