I have a form which fetches and displays data from two databases (sessions, students) and allows a curriculum manager to select different combinations of students and classes by clicking check boxes. The part that I am having trouble with is saving those different combinations back to the database "orders." I need to save the student name (from students) and the session id (from sessions) for every checkbox which the user selects. Here is a picture:

enter image description here

The above selection should make three separate entries in the orders database upon clicking "submit":

  1. Joe Biden and the SID for French
  2. Tom Riddle and the SID for French
  3. Joe Biden and the SID for Geography

Here is my code:

<form id="form-order"
              action="<?php echo JRoute::_('index.php?option=com_merakiac&task=order.save'); ?>"
              method="post" class="form-validate form-horizontal" enctype="multipart/form-data">

    <?php foreach ($session_items as $i => $item) : ?>
<div class="topb">
   <div class="sessind" id="sess_<?php echo $i; ?>"></div>
   <div class="fll"><?php echo $item->time; ?></div>
   <div class="fll"><?php echo $item->title; ?></div>
   <div class="dropbtn" data-id="<?php echo $i; ?>" id="dbtn_<?php echo $i; ?>"><span id="span_<?php echo $i; ?>">open</span></div>
<div class="moredta" id="data_<?php echo $i; ?>">
   <div class="leftc">
      <p><b>teacher:</b> <?php echo $item->teacher; ?></p></div>
   <div class="rightc">
      <p><b>level:</b> <?php echo $item->lvl; ?></p></div>
   <div class="notes">
      <p><b>notes:</b> <?php echo $item->notes; ?></p></div>

    <table class="table table-striped" id="studentList">
        <?php foreach ($student_items as $student_item) : ?>
            <tr class="row<?php echo $i % 2; ?>">
                    <input type="checkbox" name="jform[name]" value="<?php echo $student_item->name; ?>"> <?php echo $student_item->name; ?>
                    <input type="hidden" name="jform[sid]" value="<?php echo $item->sid; ?>" />
        <?php endforeach; ?>

<?php endforeach; ?>

            <div class="control-group">
                <div class="controls">

                    <?php if ($this->canSave): ?>
                        <button type="submit" class="validate btn btn-primary">
                            <?php echo JText::_('JSUBMIT'); ?>
                    <?php endif; ?>
                    <a class="btn"
                       href="<?php echo JRoute::_('index.php?option=com_merakiac&task=orderform.cancel'); ?>"
                       title="<?php echo JText::_('JCANCEL'); ?>">
                        <?php echo JText::_('JCANCEL'); ?>

            <input type="hidden" name="option" value="com_merakiac"/>
            <input type="hidden" name="task"
            <?php echo JHtml::_('form.token'); ?>
    <?php endif; ?>

The problem is that I cannot save more than one selection and it does not save with the associated sid. Only the last name selected is saved, and the last sid generated in the form is saved with it. For example, if I only selected the checkbox to book Tom Riddle for French it would instead book Tom Riddle for geography since geography is at the bottom of the form.

I need to save the name and associated sid together as a single entry in the database for every option selected. I need to save multiple bookings with one click of the submit button. Is an array the way to do this, or is that only for saving multiple values to a single column in a database?

Any help would be appreciated! Thank you!

1 Answer 1


You need to have distinguishable variables for each entry. Since you already have $i as kind of a loop counter, you can use it as an array index.

Instead of

    <input type="checkbox" name="jform[name]" value="<?php echo $student_item->name; ?>"> <?php echo $student_item->name; ?>
    <input type="hidden" name="jform[sid]" value="<?php echo $item->sid; ?>" />

just use

    <input type="checkbox" name="jform[entry][<?php echo $i; ?>][name]" value="<?php echo $student_item->name; ?>"> <?php echo $student_item->name; ?>
    <input type="hidden" name="jform[entry][<?php echo $i; ?>][sid]" value="<?php echo $item->sid; ?>" />

You will then get a single array entry, which elements again are arrays containing one name and sid each.

  • How do I write this array back to the database? I get the 'Warning: Field Required: Name, Field Required: Sid" error and nothing saves to the orders table. Do I need to modify the site/models/forms/orderform.xml file? Also, will this solution still allow me to save the name and sid as separate text columns in the orders table, or will the two be merged into one column of an array? I ask because I will later need to fetch these variables independently and use the SID as a shared key.
    – Nathanael
    Commented Feb 28, 2018 at 7:59
  • Here is what I currently have in the orderform.xml file: <field name="name" size="150" maxlength="150" filter="string" type="text" required="true" label="COM_MERAKIAC_FORM_LBL_ORDER_NAME" description="COM_MERAKIAC_FORM_DESC_ORDER_NAME" hint="COM_MERAKIAC_FORM_LBL_ORDER_NAME"/> and for the sid field <field name="sid" size="150" maxlength="150" filter="string" type="text" required="true" label="COM_MERAKIAC_FORM_LBL_ORDER_SID" description="COM_MERAKIAC_FORM_DESC_ORDER_SID" hint="COM_MERAKIAC_FORM_LBL_ORDER_SID"/>
    – Nathanael
    Commented Feb 28, 2018 at 8:02
  • 1
    Your XML file allows one name field and one sid field. If you want custom handling, you have to change the view, controller and model accordingly. -- As an alternative, look into 'Custom Fields', which supports repeatable fields.
    – nibra
    Commented Mar 1, 2018 at 0:41
  • Thank you. You answered my question in letting me know that I will need to modify the view and controller. I don't believe repeatable fields will solve my problem because, to my understanding, repeatable fields save as an array to a single row in the database instead of multiple individual rows. Please let me know if you can help me with my custom handling problem in my new question: passing an array from view to controller to model
    – Nathanael
    Commented Mar 11, 2018 at 16:56

Your Answer

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

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