This is the documentation for concrete5 version 5.6 and earlier. View Current Documentation

With page defaults, copied blocks, stacks, global areas or addons such as my Universal Content Puller, it is entirely possible that the same form block is shown on multiple pages. When a visitor to your site submits a form, knowing which page it was submitted from can be an important part of the information gathered. Some of the more advanced form addons already note the submitting page for you. Here is how you can gather the submitting page ID when using the core form block or various templates for it and derivatives of it.

You can set this up and use it on a core forms block without any further addons, though obviously as a developer I like to think you would consider my Magic Data Forms addon as a means conducting further interrogation of the form results.

For the purposes of this howto, a good place to test this out is on the 'Contact Us' form of a default contents installation of concrete 5.6.x.

First, you need to edit the page, edit the form and insert a text question to your form. I called the question 'Page', but that is not important and it could be any name you like. Save the form block, save the page, then use the developer console to look at that question and find out what id the form block has given it.

On my test, this turned out to be question12 and the corresponding table row of the default table based view.php was question_row12. This will vary between forms and form templates, but should be consistent within a form once you have added the question.

Next, we need to hide that question from visitors with some styles in an html block inserted before the actual form block. As the core forms block has a table based template, we can easily hide the entire row.

#question_row12{
  display:none;
}

If you want it to show in edit mode, you could create an edit mode template for the html block, as in Prevent an HTML block from interfering with edit mode and Format an edit mode marker.

Finally, we need to insert the page id into that form field. It can be done with a snippet of JavaScript in an html block, or my jQuickie block, placed after the form block:

$(document).ready(function(){
  if(CCM_EDIT_MODE && CCM_EDIT_MODE !== false){
    return;
  }
    $('#question_row12').hide();
    $('#Question12').val(CCM_CID);
});

The main thing this does is copy the global value CCM_CID into the form field identified as #Question12. It also makes sure the field stays hidden and leaves it alone in edit mode.

Now, when the form is submitted, amongst the form answers will be the response to the Page question, containing the cID of the page the form was submitted from.

You can copy the same form block all over a site and when you view form results you will now have an answer telling you the ID of the page the form was submitted from. If you have Magic Data Forms, you can use that answer to filter results just as you would any other form answer.

In case you are wondering about my leaving the field type as 'text' rather than changing it to 'hidden', I tried that and the form would not submit properly. Maybe there is an way to successfully make it a hidden field, but while I was having to hide the entire question, solving that problem was not important to me.

A slight word of caution. A clever visitor could hack the page value to spoof a response coming from another page. There are ways to add further protection against that by adding to the complexity of the code, but it would be a lot more work and likely over-cautious for most applications.

Read more How-tos by JohntheFish

Loading Conversation