Unable to get POST data on a single page controller view function

Permalink
Hi Everyone,

I have a controller setup on a single page, though I'm trying to get it working, via a completely separate page like so.

I have a form on the initial page like this

<form  action="<?php echo $this->url('/single-page', 'view'); ?>" enctype="multipart/form-data">


Then on the single page I have nothing, though I have a controller setup in /public_html/controllers/single-page/controller.php which starts off like this:

class singlePageController extends Controller{
   public function view(){
            var_dump($_POST['name']);
        }
    }


I am submitting with $.ajax to /single-page but the error I get is:
NULL
    <br />
    <b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /public_html/controllers/single-page/controller.php:8) in <b>/public_html/concrete/core/libraries/view.php</b> on line <b>917</b><br />


Line 8 in my custom controller.php being the var_dump of $_POST['name'].

Could anyone shed some light on what I could be doing wrong here!?

View Replies: View Best Answer
JohntheFish replied on at Permalink Reply
JohntheFish
Have you actually used the name SinglePageController? Perhaps that is confusing things. Its not clear you have all the needed parts.For a single page you need:

/controllers/my/single/page/path/controller.php
class MySinglePagePathController extends Controller{
  public function view(){
    // code as you have
  }
}


and also

/single-pages/my/single/page/path/view.php
<?php defined('C5_EXECUTE') or die("Access Denied.");
/*
Other view code as required
*/
lexbi replied on at Permalink Reply
Hi John,

Thanks for getting back to me, it appears I have the necessary steps.

Turned out I hadn't created a folder for the view.php in '/single_pages', instead I had 'single-page.php' sat in '/single_pages', I've changed that around now to use '/single_pages/single-page/view.php', though unfortunately I still get the same results.

Is it maybe me using '-' in my single page urls instead of '_'?! Or shouldn't this matter? Does a '-' break in the single page name require a sub folder? So my controller should sit in '/controllers/single/page/controller.php' ?
JohntheFish replied on at Permalink Reply
JohntheFish
The urls for the single pages should be '_' with no '-' and all lower case.

The class name must then CamelCase the characters immediately after a '/' or '_'.

For examples, download some of the free dashboard addons with single pages. Something like my 'Config Info' dashboard addon.

If you are not developing the single pages in a package, could it be you need to install them?
lexbi replied on at Permalink Reply
Hi John,

I have done as you said, though tbh, even though I was hyphenating it, I don't think that was the problem (the controller appears to be running, just can't get post data in the controller).

I am not developing this single page in a package.

This page seems to suggest that I can use $this->post for post data, though doesn't seem to work for me:
http://www.concrete5.org/documentation/how-tos/developers/basic-mvc...

Just like before I'm still getting NULL on $_POST data. I've tried a dump on these & both return NULL:
var_dump($_POST['name']);
var_dump($this->post('name'));
JohntheFish replied on at Permalink Reply
JohntheFish
$_POST is a php global, so should come straight through to you irrespective of anything c5 does.

Can you dump other globals such as $_REQUEST and $_SESSION?

What happens if you echo '<p>Hello</p>'?

Looking back at your message on header info, maybe your dump is happening before the header is sent, then lost off the top of the page when the actual page is rendered. So could it be actually happening, but you are not seeing it? A way round that is to exit; immediately after the var_dump, or to make the var_dump in the single page view file.

My usual debug clips are in:
http://www.concrete5.org/documentation/how-tos/developers/concrete5...

I also have the free addons (that use blocks, not single pages):
http://www.concrete5.org/marketplace/addons/quick-stack-view/...
http://www.concrete5.org/marketplace/addons/quick-param-view/...
lexbi replied on at Permalink Reply
If I echo 'test' at the top of the controller it comes through fine.

If I var_dump($_POST); I get a blank array.

If I var_dump($_REQUEST); I get this (This is the C5 cookie right?):

array(1) {
  ["CONCRETE5"]=>
  string(32) "8bbaf06411154253c3d2b6f6aff7d58a"
}


If I var_dump($_SESSION)l I get:
array(13) {
  ["uGroups"]=>
  array(2) {
    [2]=>
    string(1) "2"
    [1]=>
    string(1) "1"
  }
  ["accessEntitiesUpdated"]=>
  int(1398594411)
  ["uID"]=>
  string(1) "1"
  ["uName"]=>
  string(5) "admin"
  ["superUser"]=>


I think the 'view' function is normally run before the view template is rendered right? So I think you're right that it happens before the page header info loads.
Putting 'exit' at the bottom of the controller appears to stop the error which is fine! Though still no post data :(

To explain what I'm doing a little further, I am not actually using the single page "view" (just the view function), I'm trying to post to it from a completely separate page with ajax.
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
If the data is just not there, double check the network transaction for your ajax request in the browser developer console and see that the javascript has attached the post data.
lexbi replied on at Permalink Reply
Ah I think I have sorted it!

Appears that I was using some extra (unnecessary) params in the ajax call. That and passing a formData object might have also being an issue I ended up serialising it just now and seems to work.

Will have to test when I have more time later on.
lexbi replied on at Permalink Reply
As I mentioned in my previous post I think I had too many unnecessary options in my ajax call, I just stripped it back to this:

var mainFormData = $('form').serialize();
$.ajax({
           type: 'POST',
           url: "/add_timeline_hidden", // controller page
           cache: false,
           data: mainFormData,
           success: function (data, textStatus, jqXHR) {
              //console.log(jqXHR, textStatus, data);
              console.log(data);
              $('#submit').addClass('success');
           },
           error: function (jqXHR, textStatus, errorThrown) {
              console.log(jqXHR, textStatus, errorThrown);
           }
       });