7

I'm creating a simple Joomla 2.5 module that will have an html form.

mod_mymodule/tmpl/default.php:

<form method="post" id="myemailform" action="">
    <label for="ReferralName">Enter Name:</label><input type="text" name="name" value="<?php echo modCamcloudReferralHelper::getReferralName(); ?>">
    <label for="ReferralEmail">Enter Email Address:</label><input type="text" name="email">
    <label for="ReferralMessage">Enter Message (optional):</label><textarea class="message"></textarea>
    <span class="countdown"></span>
    <button type="submit" value="Send Email">Send Email</button>
    <?php echo JHtml::_('form.token'); ?>   
</form>

I have a helper class at:

mod_mymodule/helper.php - this just has some utility functions in it.

My question is what is the usual convention here to process my form on the server side. I tried to find examples of what people have done but I can't seem to find anything. Do I just put everything in the helper class:

<form method="post" id="myemailform" action="..\helper.php">

Or something like that? Thanks in advance.

Tom
  • 2,604
  • 11
  • 57
  • 96

1 Answers1

4

Yes, you should do form processing in module helper class. Keep any logic out of the template file, and you can use mod_mymodule.php to call helper methods and assign variables before including the view file.

Do not set as form action helper file! I think in your case action should be the same page, so you can also ommit action url.

Edit: As requested in the comments, this would be the content of your mod_mymodule.php

// include helper file
require_once dirname(__FILE__).'/helper.php';
// call some method of the helper class
$items = modMymoduleHelper::getItems();

$moduleclass_sfx = htmlspecialchars($params->get('moduleclass_sfx'));
// render view file from mod_mymodule/tmpl/default.php, $items is available in the view file
require JModuleHelper::getLayoutPath('mod_mymodule', $params->get('layout', 'default'));
Marko D
  • 7,576
  • 2
  • 26
  • 38
  • Since my helper file is not the same directory wouldn't I have to specify the helper file with form action? Not sure I understand the mechanics of this otherwise? Can you provide a simple example? Thanks. – Tom Feb 25 '13 at 15:14
  • When you submit form to the same page, your module will be loaded again. Joomla will call `mod_mymodule.php`, which should be in charge of calling helper methods, and passing neccessary variables to the view `mod_mymodule/tmpl/default.php`. `my_module.php` includes both the view file, and communicates with helper class, Joomla does not call any other php file when it includes your module – Marko D Feb 25 '13 at 15:19
  • I added an example of `mod_mymodule.php` – Marko D Feb 25 '13 at 15:27
  • Ah perfect, I think I get it. So I can just add `modMymoduleHelper::processForm();` to process the form in `mod_module.php` which will then call `processForm()` function in my helper class. Is that right? – Tom Feb 25 '13 at 15:32
  • One more question for you: `modMymoduleHelper::processForm();` will load everytime (as you said). How do I call this only if my form submit button is pressed? – Tom Feb 26 '13 at 13:13
  • Check `if (isset($_POST['myinputname'])) {}` – Marko D Feb 26 '13 at 13:14