joomla module development with form - how to process
Asked Answered
W

1

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.

Woodchuck answered 25/2, 2013 at 2:42 Comment(0)
D
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'));
Donkey answered 25/2, 2013 at 8:28 Comment(6)
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.Woodchuck
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 moduleDonkey
I added an example of mod_mymodule.phpDonkey
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?Woodchuck
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?Woodchuck
Check if (isset($_POST['myinputname'])) {}Donkey

© 2022 - 2024 — McMap. All rights reserved.