Forums / Developer / Custom form action

Custom form action

Author Message

Pål J Didriksen

Friday 08 July 2005 2:31:50 pm

I have made an extension module that handles custom forms. This enables me to put action buttons in templates, that can trigger off any desired action in the php-script. Hope this can be useful for someone else!

The extension is based on the original form funconality in eZPublish' kernel. It must be used with caution, as it takes arguments that are embedded in the html-source, which can easily be manipulated by a hacker.

In my project, however, I use the form only for administrative tasks, so the module is only available for users with administrator privileges.

<b>Files:</b>
1. extension/myextension/modules/mymodule/process.php

<?php

include_once( "kernel/common/template.php" );
include_once( "lib/ezutils/classes/ezhttptool.php" );

$Module =& $Params['Module'];

$ini =& eZINI::instance();
$isEnabled = $ini->variable( 'FormProcessSettings', 'Module' ) == 'enabled';
if ( !$isEnabled )
{
    return $Module->handleError( EZ_ERROR_KERNEL_MODULE_DISABLED, 'kernel',
                                 array( 'check' => array( 'view_checked' => false,
                                                          'module' => 'form' ) ) );
}

$tpl =& templateInit();

// Parse HTTP POST variables
$formProcessed = false;

$http =& eZHTTPTool::instance();
$postVariables =& $http->attribute( 'post' );

$action = $postVariables['action'];
// additional input variables from the form are available in $postVariables
// $myVariable = $postVariables['my_variable']

// $redirectURL - address to go to after processing is finished
$redirectURL = $postVariables['redirect'];

switch ($action)
{
    case "my_action1":
    {
        // Add code to execute for forms identified as "my_action1"
    }break;

    case "my_action2":
    {
        // Add code to execute for forms identified as "my_action2"
    }break;

    default :
    {
        // No action submitted
    }break;
}

$Module->redirectTo( $redirectURL );

?>

2. extension/myextension/modules/mymodule/module.php

<?php
$Module = array( "name" => "MyModule" );

$ViewList = array();
$ViewList["process"] = array(
    "script" => "process.php" );

?>

3. extension/myextension/settings/module.ini.append

[ModuleSettings]
ExtensionRepositories[]=mymodule

4. Template code
To use the extension, you need this code in your templates:


<form method="post" action={"/mymodule/process"|ezurl}>
    <input name="action" type="hidden" value="my_action1" />

    {* Redirect to current node *}
    <input name="redirect" type="hidden" value="{$node.url}" />

    Enter value:
    <input name="my_variable" type="text" />

    <input class="button" type="submit" name="MyButton" value="Execute my_action1" />
</form>

Now, remember to activate your extension in the admin interface, Settings -> Extensions.

Hope I didn't forget anything... Comments and suggestions are appreciated!

Pål J

Steve P

Saturday 09 July 2005 2:29:07 am

Thank you - this looks interesting & I'll have a play...

You are obviously pretty experienced so wonder if you can suggest a way for me to update my users from a script? I would like to update a field "payment_status" automatically on completion of a shop transaction ie to "paid_o5" or similar.

Best wishes

Steve

David Eriksson

Monday 11 July 2005 7:14:44 am

Have a look at workflows Steve. They enable you to automatically run a php script every time something happens, for example at a shop checkout.

/David

Maxime Thomas

Friday 03 February 2006 2:28:44 am

I've got some Kernel (22) Errors : Module is disabled.

To fix that, put into your override site.ini.append.php :

[FormProcessSettings]
Module=enabled

Maxime Thomas
maxime.thomas@wascou.org | www.wascou.org | http://twitter.com/wascou

Company Blog : http://www.wascou.org/eng/Company/Blog
Technical Blog : http://share.ez.no/blogs/maxime-thomas

eZ debug

Timing: Jan 18 2025 22:28:16
Script start
Timing: Jan 18 2025 22:28:16
Module start 'content'
Timing: Jan 18 2025 22:28:16
Module end 'content'
Timing: Jan 18 2025 22:28:16
Script end

Main resources:

Total runtime0.1624 sec
Peak memory usage2,048.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0063 587.6094180.8438
Module start 'content' 0.00630.0055 768.453197.9609
Module end 'content' 0.01180.1505 866.4141533.2344
Script end 0.1623  1,399.6484 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00352.1409200.0002
Check MTime0.00140.8443200.0001
Mysql Total
Database connection0.00080.495710.0008
Mysqli_queries0.116671.82201410.0008
Looping result0.00130.79001390.0000
Template Total0.150192.510.1501
Template load0.00080.502610.0008
Template processing0.149391.947110.1493
Override
Cache load0.00050.335110.0005
Sytem overhead
Fetch class attribute can translate value0.00080.492710.0008
XML
Image XML parsing0.00020.143010.0002
General
dbfile0.00311.9295200.0002
String conversion0.00000.002530.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0002 secs