Welcome Guest, Not a member yet? Register   Sign In
Why doesn't this work ? <solved>
#1

[eluser]charlie spider[/eluser]
I wanted to take all of the code for processing a lengthy form and place it into it's own controller, thus keeping it separate from all of the code that gathers all of the data and views necesary to present the form (plus a bunch of other stuff on the page).

To boil it down to the most simple example possible of what I am trying to do...

here is the form controller : test_form.php

Code:
&lt;?php
class Test_form extends Controller
{

    function Test_form()
    {
        parent::Controller();    
        $this->load->helper('form');
    }
    
    function index()
    {    
        $this->load->view('test_form_view');
    }

}


the form view : test_form_view.php

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Test Form&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;?php echo form_open('test_redirect'); ?&gt;

<h5>Enter Anything!!!</h5>
&lt;input type="text" name="some_text_field" value="" size="50" /&gt;

<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;


which submits the form to a different controller : test_redirect.php

(this is where I want to process the form data, but instead of calling a view from here, I ultimately want to be able to redirect back to the first controller with error or success messages sent via flash data)

Code:
&lt;?php
class Test_redirect extends Controller
{

    function Test_redirect()
    {
        parent::Controller();    
        $this->load->helper( 'url' );
    }
    
    function index()
    {    
        redirect('test_success', 'refresh');
    }

}


which attempts to redirect to a success page : test_success.php

Code:
&lt;?php
class Test_success extends Controller
{

    function Test_success()
    {
        parent::Controller();    
    }
    
    function index()
    {    
        echo '<h1>IT WORKS</h1><br />';        
        die();
    }

}


but results in the following error:

Code:
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test_site\application\controllers\test_redirect.php:1)

Filename: helpers/url_helper.php

Line Number: 539

which (if you're like me and couldn't be bothered to scoll all the way over to the left) says that headers were sent on line 1 of test_redirect.php !!!

there is no white space at the beginning or end of any of the files.

Why doesn't this work ?
#2

[eluser]srpurdy[/eluser]
Strange, copied your code, works as you intended.
http://www.purdydesigns.com/test_form/

Maybe one of your libraries/helpers have some white space?

It seems to be complaining about url_helper.php so maybe look in that file.
Shawn
#3

[eluser]charlie spider[/eluser]
what version of CI ?

just updated my system folder with a fresh copy of CI 1.7.2
to see if that would help
and simplified the test_form.php even more by not using the form helper

but still getting the same error.
#4

[eluser]srpurdy[/eluser]
[quote author="charlie spider" date="1272521426"]what version of CI ?

just updated my system folder with a fresh copy of CI 1.7.2
to see if that would help
and simplified the test_form.php even more by not using the form helper

but still getting the same error.[/quote]

1.7.2 also,

looking at line 539 of url_helper.
Code:
if ( ! function_exists('redirect'))
{
    function redirect($uri = '', $method = 'location', $http_response_code = 302)
    {
        if ( ! preg_match('#^https?://#i', $uri))
        {
            $uri = site_url($uri);
        }
        
        switch($method)
        {
            case 'refresh'    : header("Refresh:0;url=".$uri); // <- line 539
                break;
            default            : header("Location: ".$uri, TRUE, $http_response_code);
                break;
        }
        exit;
    }
}

seems to running into the problem there I guess, maybe try a different redirect like
Code:
redirect('test_success');
or
Code:
redirect('test_success', true);
#5

[eluser]srpurdy[/eluser]
oh another thing to try

is to add ?&gt; close on the end of your controllers. I've had that happen before for some weird reason.
#6

[eluser]charlie spider[/eluser]
OK

fresh download of CI 1.7.2 into a new folder on the server

crunched the files down to:

test_form.php
Code:
&lt;?php class Test_form extends Controller{
function Test_form(){parent::Controller();}
function index(){$this->load->view('test_form_view');}
}?&gt;

test_form_view.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Test Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action="http://localhost/test/test_redirect" method="post"&gt;
<h5>Enter Anything!!!</h5>
&lt;input type="text" name="some_text_field" value="" size="50" /&gt;
<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

test_redirect.php
Code:
&lt;?php class Test_redirect extends Controller{
function Test_redirect(){parent::Controller();$this->load->helper( 'url' );}
function index(){redirect('test_success', 'refresh');}
}?&gt;

test_success.php
Code:
&lt;?php class Test_success extends Controller{
function Test_success(){parent::Controller();}
function index(){echo '<h1>IT WORKS</h1><br />';die();}
}?&gt;

and here's the error:
Code:
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\system\application\controllers\test_redirect.php:1)

Filename: helpers/url_helper.php

Line Number: 539
#7

[eluser]WanWizard[/eluser]
The error message says that line 1 of test_redirect.php produced output. Sure there's no space before your &lt;?php?
#8

[eluser]charlie spider[/eluser]
Quote:Sure there’s no space before your &lt;?php?

no spaces.

I've even rewritten the file in another text editor in case some kind of hidden character was being added. Still threw errors.

I've also uploaded this to my live server and it still throws errors:

www.pyfo.ca/test/test_redirect

the above link is using a freshly downloaded, unadultered copy of CI 1.7.2, of which the only thing I have changed is the base_url value in the main config file, as well as adding the test_redirect.php and test_success.php files (and a super basic htaccess file).

my development and my live servers are both using 5.2 versions of PHP

has anybody else tried running my test_redirect.php file on their system, and if so, what version of PHP are you running ?
#9

[eluser]WanWizard[/eluser]
Just dropped your test_redirect.php example in my controllers directory, and added a route to it. Loads and redirects without an error.

To double check, I've setup a new virtual host, copied a fresh CI 1.7.2. into it, copied my standard .htaccess to it, changed the base_url and removed the index.php, and created the test_redirect and test_success controllers.

Result: IT WORKS. PHP on my development machine is v5.3.2, but I don't think that's the issue...
#10

[eluser]cahva[/eluser]
Check that your view files arent UTF-8 with BOM(byte order mark). That would cause output error on line 1.




Theme © iAndrew 2016 - Forum software by © MyBB