TYPOlight tutorials
Table of Contents
TYPOlight Tutorial - Form processing using custom PHP script
This tutorial briefly shows how to access a form's variables after the user has clicked submit.
Set-up your Processing Page
Add a page using TYPOlight's Site structure module. Give the page a name, make sure its published and important make sure that you check the box for Hide from Navigation. This will ensure that your script page is not accidentally run.
Set-up your Form
Add a form using TYPOlight's Form generator. Make sure your Jump to page is pointing to the Processing Page above. This is the page that the form will redirect to once its done with the basics like validation checks, etc.
I've also discovered that you can add hidden form fields to your form and they will pass to the php script via TL's form processing routines. Just add an HTML element just after the submit button (easier to spot there, or you can order it anywhere within the form). This is great, because if you add this at the TOP of a checkbox, you can force a checkbox to ALWAYS return a, e.g. FALSE value, no matter if it was selected or not.
Set-up your Article
Add an Article using TYPOlight's Articles. You can add a Text Content Element (CE) as this is the easiest one. Start typing some introduction text, e.g. Hello World. Then below that enter the tag {{file::processform.php}}. You can find out more about insert tags here.
Set-up your PHP Script
Create a script using your favorite text/PHP editor and store it in the (typolight root)/templates/ folder in TYPOlight root.
TYPOlight will only run a PHP file if its stored in (typolight root)/templates/ folder location.
Inside the script, e.g. processform.php you can process the form variables.
<?php // process each form variable foreach ($_SESSION['FORM_DATA'] as $key=>$value) { printf('Form Field: %s = [%s] <br />', $key, htmlentities($value)); } ?>
TYPOlight destroys the $_POST variables. The only way to access the form variables is using the $_SESSION['FORM_DATA'].
If you are clever enough to write a front-end template or module, then you can access the form variables like this:
<?php // within a .TPL file or Front-End module (minus the PHP at the top in that case) foreach ($this->Input->post as $key=>$value) { printf('Form Field: %s = [%s] <br />', $key, htmlentities($value)); } ?>
OR, one at a time like this
<?php // within a .TPL file or Front-End module (minus the PHP at the top in that case) echo $this->Input->post('email'); ?>
Thanks
Thanks to:
- leo, for his unrelated post description in the forum.
Tutorial created by thyon from thyon design