How can I save multiple dynamic values using check boxes?

by Nathanael   Last Updated February 26, 2018 08:10 AM

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>
<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">
        <tbody>
        <?php foreach ($student_items as $student_item) : ?>
            <tr class="row<?php echo $i % 2; ?>">
                <td>
                    <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; ?>" />
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>

</div>
<?php endforeach; ?>

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

                    <?php if ($this->canSave): ?>
                        <button type="submit" class="validate btn btn-primary">
                            <?php echo JText::_('JSUBMIT'); ?>
                        </button>
                    <?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'); ?>
                    </a>
                </div>
            </div>

            <input type="hidden" name="option" value="com_merakiac"/>
            <input type="hidden" name="task"
                   value="orderform.save"/>
            <?php echo JHtml::_('form.token'); ?>
        </form>
    <?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!



Related Questions


passing an array from view to controller to model

Updated March 11, 2018 17:10 PM

Need help writing a form to a table

Updated February 24, 2018 10:10 AM