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:
The above selection should make three separate entries in the orders database upon clicking "submit":
- Joe Biden and the SID for French
- Tom Riddle and the SID for French
- 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!