Post values after the form is submitted

Permalink 1 user found helpful
I've built a custom form. How do I grab $_POST['Question1'], $_POST['Question2'], etc variable values from the form after I submit it? That can't be that my only option is to look in the database after the fact.

Running latest version c5.
ver 5.7.5.13

 
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi evka,

By custom form, do you mean a custom form block, External Form block, a form added to a single page, or something else?
evka replied on at Permalink Reply
Yes, sorry. It's a block template based on "Form Tableless Layout" add on.
MrKDilkington replied on at Permalink Best Answer Reply
MrKDilkington
@evka

When you submit the Form block, the form uses the "submit_form" action to process the form using the action_submit_form() method in the controller.
https://github.com/concrete5/concrete5/blob/5.7.x/web/concrete/block...
https://github.com/concrete5/concrete5/blob/5.7.x/web/concrete/block...

Example:
<form enctype="multipart/form-data" class="form-stacked miniSurveyView" id="miniSurveyView128" method="post" action="http://localhost/my_site/index.php/contact/submit_form/128#formblock128">
    <!-- form contents -->
</form>

The action_submit_form() controller method that processes the submission:
https://github.com/concrete5/concrete5/blob/5.7.x/web/concrete/block...

In action_submit_form(), you can see the $_POST superglobal being used to get the "qsID" key from the array.
https://github.com/concrete5/concrete5/blob/5.7.x/web/concrete/block...

I believe that using superglobals directly is frowned on, instead post() should be used instead.
Example:
$this->post('your_post_array_key');
// or
$this->request->post('your_post_array_key');
evka replied on at Permalink Reply
Thank you for this detailed response. However, I still have a small problem. My controller is not calling action_submit_form($bID = false) function for some reason. Is it because the controller class extends Package rather than BlockController? Here's my brief example

use Concrete\Core\Block\BlockController;
class Controller extends Package
{
//some methods pertaining to package

public function action_submit_form($bID=false)
{
throw new Exception(t("Congratulations you're inside controller method!"));
}
}
MrKDilkington replied on at Permalink Reply
MrKDilkington
@evka

Your block controller should extend BlockController.

Here is the documentation related to interactive blocks:
https://documentation.concrete5.org/developers/working-with-blocks/c...
evka replied on at Permalink Reply
@MrKDilkington

I hate to trouble you, but I am still having a hard time with this. I tried to understand interactive blocks, but I couldn't. I placed the controller.php to mysite/packages/my_package/blocks/form

inside controller.php I made sure to include
class Controller extends BlockController
{
public function action_submit_form($bID=false)
{
throw new Exception(t('Finally, calling the method.'));
}
}

However, action_submit_form is not firing. Maybe you can point me to a package that implements this type of interactive block functionality between a Package and BlockController? Much appreciate your time.
Gondwana replied on at Permalink Reply
Gondwana
This might help...
https://www.concrete5.org/marketplace/addons/contact-form-no-links1...
...or it might not. It ended up getting rather kludgy.
evka replied on at Permalink Reply
@Gondwana

Great! I'll try it. Thank you.
evka replied on at Permalink Reply
@Gondwana
I looked through it, but it did not work out.

I am trying to capture form values in my view.php file after submit button is clicked.
my_package/blocks/form/templates/my_package/view.php

I've tried to capture $_POST values in controller.php, but it is not working out regardless where I put it. I've even tried echoing form data in the core controller.php located in concrete/blocks/form/controller.php

This is really frustrating. I want to move forward and work on more complex stuff, but I can't get past simple capturing of form data. I am able to do this in Wordpress and Drupal much easier...just saying.

I could try pulling data from the database after it's submitted, but I am not seeing any unique IDs every time the form is submitted. The IDs (qsID, bsID, etc) seem to be repeating.

I am just born to be confused...I guess.
evka replied on at Permalink Reply
@MrKDilkington
What is 'your_post_array_key' ?

In the context of a typical form, I've tried 'Question1' and 'first_name'. It seems that $this->post() and $_POST are null after I hit the submit button.

Basically, I can create the entire form template with my own custom js and css, but if I need to grab form data after it's submitted, it is not possible, unless I completely scrap all the work up to this point and move away from the whole package-template idea.

my_package/blocks/form/templates/my_package/
MrKDilkington replied on at Permalink Reply
MrKDilkington
@evka

I was using "your_post_array_key" as a descriptive placeholder for the $_POST superglobal array key.

Example: accessing $_POST directly
$qsID = intval($_POST['qsID']);

https://github.com/concrete5/concrete5/blob/5.7.x/web/concrete/block...

The code above could be replaced using $this->post() or $this->request->post():
$qsID = intval($this->post('qsID'));
$qsID = intval($this->request->post('qsID'));

If the post() method is not passed a $_POST array key, it returns the full $_POST array.

One way to test this and debug the values submitted to your form is by step debugging or using the dd() helper method (a combination of var_dump() and die()).
- add this code to the top of action_submit_form()
dd($this->post());

- fill out your form and submit it
- when action_submit_form() is called and starts processing the request, it will stop execution and var_dump() the $_POST array
- here is an example of what you might see
array(8) {
  ["Question1"]=>
  string(12) "Buffalo Bill"
  ["Question2"]=>
  string(7) "Johnson"
  ["Question3"]=>
  string(17) "lotion@basket.com"
  ["Question4"]=>
  string(17) "I love your work."
  ["Question5"]=>
  string(22) "I need my lotion back."
  ["Submit"]=>
  string(6) "Submit"
  ["qsID"]=>
  string(10) "1486333558"

The same thing can be done if you want to get a specific $_POST array key value pair.
evka replied on at Permalink Reply
@MrKDilkington

When I add dd($this->post());
in concrete/blocks/form/controller.php action_submit_form() method
nothing happens. I know form is submitting because I am getting an email with form data.

If I add dd($this->post()); in my
my_package/blocks/form/templates/my_package/view.php after $success variable, I get
Missing argument 1 for Concrete\Core\View\AbstractView::post(),
MrKDilkington replied on at Permalink Reply
MrKDilkington
@evka

I recommend reviewing your code for errors and issues.

After you submit the form and it is processed, the page is redirected. I don't believe the submission values are forwarded to the redirect page. On initial view, I am not sure that the Form block view has any $_POST values available.

You received the error because class BlockView extends AbstractView and AbstractView's post() method requires a $_POST array key as the argument.
http://docs.mnkras.com/class_concrete_1_1_core_1_1_view_1_1_abstrac...