The new extension does not really do anything just yet. The PHP file has to be edited to our needs. The file to edit is pi1/class.user_test_pi1.php. To edit the file take the following steps.
Go to the Extension Manager and click on your extension.
From the dropdown menu at the top right select 'Edit Files' and your screen will change to this view:
Now select the following file for editing : pi1/class.user_test_pi1.php.
Below is a copy of the original file. Here is a link to the file. I also saved it as a txt file. Here is a link to the file.
I had to split rows to fit the text on the page. You will notice the difference when you look at your own copy. When I opened the file in Dreamweaver the code showed up in different colors, so I decided to duplicate the colors to make the code more easily understandable. I also added line numbering.
1 <?php 2 /*************************************************************** 3 * Copyright notice 4 * 5 * (c) 2003 Marlies Cohen (mcu(at)mcuniverse.com) 6 * All rights reserved 7 * 8 * This script is part of the Typo3 project. The Typo3 project is 9 * free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by11 * the Free Software Foundation; either version 2 of the License, or12 * (at your option) any later version.13 * 14 * The GNU General Public License can be found at15 * http://www.gnu.org/copyleft/gpl.html.16 * 17 * This script is distributed in the hope that it will be useful,18 * but WITHOUT ANY WARRANTY; without even the implied warranty of19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the20 * GNU General Public License for more details.21 *22 * This copyright notice MUST APPEAR in all copies of the script!23 ***************************************************************/24 /** 25 * Plugin 'Marlies Test' for the 'user_test' extension.26 *27 * @author Marlies Cohen <mcu(at)mcuniverse.com>28 */ 31 require_once(PATH_tslib."class.tslib_pibase.php"); 33 class user_test_pi1 extends tslib_pibase {34 var $prefixId = "user_test_pi1"; // Same as class name35 var $scriptRelPath = "pi1/class.user_test_pi1.php"; // Path to this script relative to the extension dir.36 var $extKey = "user_test"; // The extension key. 38 /**39 * [Put your description here]40 */41 function main($content,$conf) {42 $this->conf=$conf;43 $this->pi_setPiVarDefaults();44 $this->pi_loadLL(); 46 $content='47 <strong>This is a few paragraphs:</strong><BR>48 <P>This is line 1</P>49 <P>This is line 2</P> 51 <h3>This is a form:</h3>52 <form action="'.$this->pi_getPageLink($GLOBALS["TSFE"]->id). '" method="POST">53 <input type="hidden" name="no_cache" value="1">54 <input type="text" name="'.$this->prefixId.'[input_field]" value="'.htmlspecialchars($this->piVars["input_field"]).'">55 <input type="submit" name="'.$this->prefixId.'[submit_button]" value="'.htmlspecialchars($this->pi_getLL("submit_button_label")).'">56 </form>57 <BR>58 <P>You can click here to '.$this->pi_linkToPage("get to this page again",$GLOBALS["TSFE"]->id).'</P>59 '; 61 return $this->pi_wrapInBaseClass($content);62 }63 }67 if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"] ["ext/user_test/pi1/class.user_test_pi1.php"]) {68 include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"] ["ext/user_test/pi1/class.user_test_pi1.php"]);69 }71 ?> |
The editing of the PHP file takes place in the 'Content' section, lines 46-59:
46 $content='47 <strong>This is a few paragraphs:</strong><BR>48 <P>This is line 1</P>49 <P>This is line 2</P> 51 <h3>This is a form:</h3>52 <form action="'.$this->pi_getPageLink($GLOBALS["TSFE"]->id). '" method="POST">53 <input type="hidden" name="no_cache" value="1">54 <input type="text" name="'.$this->prefixId.'[input_field]" value="'.htmlspecialchars($this->piVars["input_field"]).'">55 <input type="submit" name="'.$this->prefixId.'[submit_button]" value="'.htmlspecialchars($this->pi_getLL("submit_button_label")).'">56 </form>57 <BR>58 <P>You can click here to '.$this->pi_linkToPage("get to this page again",$GLOBALS["TSFE"]->id).'</P>59 '; |
First remove lines 47-49 and lines 57-58. Then view your sample form again and it will now look like this:
Line 46
All the content which is going to be outputted is collected within this variable.
$content
Line 52
The action form field inserts the URL return by an API call which takes the ID of the page as parameter. That means basically the form submits to itself.
<form action="'.$this->pi_getPageLink($GLOBALS["TSFE"]->id). '" method="POST">
Line 53
This plugin acts as a non-cached object as seen by this line of code:
<input type="hidden" name="no_cache" value="1">
Line 54
Then there is a text field that is prefixed by and ID. Kasper suggests that you do that consistently because then you are sure that your form fields are uniquely named. The second part of that line defines the value of the field.
<input type="text" name="'.$this->prefixId.'[input_field]" value="'.htmlspecialchars($this->piVars["input_field"]).'">
Line 55
This line defines the submit button
<input type="submit" name="'.$this->prefixId.'[submit_button]" value="'.htmlspecialchars($this->pi_getLL("submit_button_label")).'">
And that is all the code needed for the form to start with.
Line 54
Currently it has the value of input field. That should be changed 'title' in the first part of the line.
<input type="text" name="'.$this->;prefixId.'[title]"
Then the value of that field comes from the second part of the line.
value="'.htmlspecialchars($this->piVars["input_field"]).'">
This part still points to 'input_field' and needs to be changed. It can be change to be left blank. Also place a 'DATA' field inside the key of this area. Then the whole new Line 54 will look like this:
<input type="text" name="'.$this->prefixId.'[DATA][title]' value="'>
Textarea
Now we need to add a new line for the text area and give it a name. The easiest way to do this is by copying the prefixId for the title and changing 'title' to 'description'.
<textarea name="'.$this->prefixId.'[DATA][description]"></textarea>
Line 55
The submit button line needs to be changed from this:
<input type="submit" name="'.$this->prefixId.'[submit_button]" value="'.htmlspecialchars($this->pi_getLL("submit_button_label")).'">
to this:
<input type="submit" name="'.$this->prefixId.'[submit_button]" value='SEND FORM'>
Now save your file and view it.
Check out the Source Code for the form and you will find it looks like this:
<H3>This is a form:</H3> <form action="/index.php/Form_Extension_Test_Page/770/0/" method="POST"> <input type="hidden" name="no_cache" value="1"> <input type="text" name="user_test_pi1[DATA][title]" value=""> <textarea name="user_test_pi1[DATA][description]"></textarea> <input type="submit" name="user_test_pi1[submit_button]" value="Click here to submit value"> </form> |
Analysis of the Source Code gives us the following information:
form action:
<form action="/index.php/Form_Extension_Test_Page/770/0/"
title has the extension key prefixed
<input type="text" name="user_test_pi1[DATA][title]" value="">
textarea has the extension key prefixed
<textarea name="user_test_pi1[DATA][description]"></textarea>
submit button has the extension key prefixed and the form submits itself to the same page.
<input type="submit" name="user_test_pi1[submit_button]"
value="Click here to submit value">
To debug the form and to get a hold of the data that is submitted place this code above the $content
t3lib_div::debug($this->piVars):
This API call will output the content of the incoming values. The reason why I show you this is because this function call will automatically grab the content submitted to this extension. The content is in the name space of the prefixId which we applied. The idea is that if any content is submitted within this name space it will be available in the internal variable called piVars. If we use this function to just output this content we can take a visual look at it. Save and view the page and resubmit the page you will see the data from the form is neatly organized. Actually we could us this value to detect if the form is submitted.
Note:
Keep the debug code while you are testing your form. Once you are satisfied that everything works the way it should place a # sign before that line of code to prevent it from being executed.
What's needed now is a condition. Use 'isset' if you want to make changes to the submit_button label afterwards.
if(isset'.$this->piVars{submit_button']) { // WHAT TO DO if content is submitted t3lib_div::debug($this->piVars);} else {
Next all the content has to be collected into the variable 'content'. Our code now looks like this:
if(isset'.$this->piVars{submit_button']) { // WHAT TO DO if content is submitted t3lib_div::debug($this->piVars);} else { $content='<h3>This is a form:</h3><form action="'.$this->pi_getPageLink($GLOBALS["TSFE"]->id).'" method="POST"><input type="text" name="'.$this->prefixId.'[DATA][title]" value=""><textarea name="'.$this->prefixId.'[DATA][description]"></textarea><input type="submit" name="'.$this->prefixId.'[submit_button]" value="SEND FORM!"></form>
} |
Now we have to add a query and use an API function. We use 'INSERT INTO' and then the database table name. To find the table name open a new window --> go to the extension manager --> open your extension --> scroll down to 'database requirements' tables and copy that name (user_test_testing) in this example.
It is also important to place these values on the right page and so we add the pid field
$query = 'INSERT INTO user_test_testing (title, description, pid) VALUES ("'.addslashes($this->piVars['DATA']['title']).'","'.addslashes($this->piVars['DATA']['description']).'", "'.$GLOBALS['TSFE']->id.'")';$res = mysql(TYPO3_db, $query);echo mysql_error();
For the title field insert the DATA key and 'title' key and always remember to include 'addslashes'.
For the description field copy the code for the title key and change title to description.
Next insert the page ID code
"'.$GLOBALS['TSFE']->id.'";
To redirect the page use an API function. The function is on the webserver in this file in the Typo3 folder: typo3\t3lib\class.t3lib_div.php.
function locationHeaderURL($path)
This is the code you are using:
header ('Location: '.t3lib_div::locationHeaderURL('index.php?id=123'));
Id=123 is the page where something has to be displayed: like congratulations you did it or a join page or something like that. It doesn't contain any info about the new record ID or anything. In this tutorial I create a 'Thank You' page. So when you use the 'Test Page' after you submit the form you end up on the 'Thank You' page.
Here is the final version of the code. I am only showing the actual code part here, not the whole file. I also placed a copy of the file as a text file here.
Note:
All the code in red will be different for each extension you create.
require_once(PATH_tslib."class.tslib_pibase.php");class user_test_pi1 extends tslib_pibase { var $prefixId = "user_test_pi1"; // Same as class name var $scriptRelPath = "pi1/class.user_test_pi1.php"; // Path to this script relative to the extension dir. var $extKey = "user_test"; // The extension key. /** * [Put your description here] */ function main($content,$conf) { $this->conf=$conf; $this->pi_setPiVarDefaults(); $this->pi_loadLL();if(isset($this->piVars['submit_button'])) { // WHAT TO DO if content is submitted# t3lib_div::debug($this->piVars);$query = 'INSERT INTO user_test_testing (title,description,pid) VALUES("'.addslashes($this->piVars['DATA']['title']).'", "'.addslashes($this->piVars['DATA']['description']).'", "'.$GLOBALS['TSFE']->id.'")';$res = mysql(TYPO3_db, $query);echo mysql_error();header ('Location: '.t3lib_div::locationHeaderUrl('index.php?id=772'));} else { $content='<H3>This is a form:</H3><FORM action="'.$this->pi_getPageLink($GLOBALS[" TSFE?]->id).'" method="POST"><INPUT name="'.$this->prefixId.'[DATA][title]"><TEXTAREA name="'.$this->prefixId.'[DATA][description]"></TEXTAREA><INPUT type=submit value="Submit Query" name="'.$this->prefixId.'[submit_button]" value="SEND FORM!"></FORM> ';} return $this->pi_wrapInBaseClass($content); }}if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/user_test/pi1/class.user_test_pi1.php"]) { include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/user_test/pi1/class.user_test_pi1.php"]);} |
Ok, now we are all done. Let's do a final testing of the form. Go to the test page and fill in the info. Screen shot of test page with form filled in but not sent.
After clicking on the SEND FORM! button I end up on the Thank You page I created for this tutorial. So I know that the form submitted.
Now to check if a new record has bee added to the files, go to List and then select your 'Test Page' and you will see this screen
To totally satisfy our curiosity click on ID3 in the list under the title 'Test Table' (2) and you will see the form we just sent.
Everything worked just fine.