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
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:
I am submitting with $.ajax to /single-page but the error I get is:
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!?
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!?
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' ?
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' ?
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?
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?
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:
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:
$_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/...
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/...
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?):
If I var_dump($_SESSION)l I get:
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.
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"]=>
Viewing 15 lines of 44 lines. View entire code block.
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.
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.
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.
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.
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); } });
and also