2

Am creating a simple custom search , where users will search using a droplist of option , i have successfully created the module , however i do not know how to show the form in frontend and how to pass values to my helper file for validation , please help ! Below is my code

model(helper.php)

public static function getdata($catid)
    {
          //$catid1=17;
           $res=array();
           $categories = JCategories::getInstance('Content');
           $cat = $categories->get($catid);
           $children = $cat->getChildren();
           $res['FieldofStudy']=$children;

        return $res;
    }

View (tmpl/default.php) Which is form that shows droplist to send back to controller

            <select id="Field-of-Study" class="form-control">
                <?php foreach($res['FieldofStudy'] as $a){  ?>
                <option>
                <?php  echo $a->title ;  ?>
                </option>
                <?php } ?>
              </select>
4
  • Please show some code. I assume you have already created the HTML markup for the form right?
    – Lodder
    Dec 23, 2015 at 15:57
  • @Lodder see new edit with code Dec 23, 2015 at 16:12
  • @Lodder or should i use xml file in tmpl folder to define form as in plugin development ? Dec 23, 2015 at 16:17
  • I'm writing my answer now. 2 minutes
    – Lodder
    Dec 23, 2015 at 16:17

1 Answer 1

2

Personally, I would do it like this:

Lets say you have a few form fields (for example, 2 inputs and a select box):

<form method="post" name="shout">

    <input name="myform[name]" type="text">

    <input name="myform[surname]" type="text">

    <select name="myform[study]" id="Field-of-Study" class="form-control">
        <?php foreach($res['FieldofStudy'] as $a) : ?>
            <option><?php echo $a->title; ?></option>
        <?php endforeach; ?>
    </select>

</form>

Notice I've used myform in the name="" attribute.

Now, in your mod_mymodule.php file, you can check if the post if set and get the entire post array from your form like so:

if (isset($_POST))
{
    $post = $app->input->post->get('myform', array(), 'array');

    // You can now access each field value using
    // $post['name']
    // $post['surname']
    // $post['study']
}

To put the post into a function in the helper.php, you first need to call the helper like so:

$helper = new ModMYMODULEHelper();

Then call the function from the helper

$helper->myfunction($post);

Hope this helps

6
  • @jimmyobonyo - You're welcome ;)
    – Lodder
    Dec 23, 2015 at 16:27
  • Just one more thing , lets say i get the fillterd values in the helper file , and i want this values to be displayed in another the main page ie not the modules view how would i go about this please Dec 23, 2015 at 16:29
  • You mean somewhere else on the site? in which case, I'd most likely use JSession and push the value to a session. For a more in-depth answer, I'd suggest asking a new question though
    – Lodder
    Dec 23, 2015 at 16:33
  • No not somewhere else but , lets say the form is floated left in the page , i want to display the form in main page view ie the center page Dec 23, 2015 at 16:35
  • Oh I see. You can assigned your module a custom position, then in an article, use {loadposition MYPOSITION} to call your module
    – Lodder
    Dec 23, 2015 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.