Right her is an extract of my code
At the top (before the HTML tag
PHP:
<?php
require_once 'session.php';
require_once 'conn.php';
/* Code to get username to display on HTML page */
$acc_query = $conn->query("SELECT * FROM `admin` WHERE `admin_id` = '$_SESSION[admin_id]'") or die(mysqli_error($conn));
$acc_fetch = $acc_query->fetch_array();
$acc_name = $acc_fetch['username'];
if (isset($_GET["class_id"])) {
// First get class description to display on form AND class_id
$c_query = $conn->query("SELECT * FROM `classdetails` WHERE `class_id`= '$_REQUEST[class_id]'") or die(mysqli_error($conn));
$c_fetch = $c_query->fetch_array();
$class = $c_fetch['class_id'];
$class_name = $c_fetch['class_name'];
// Now get list of members NOT in class - used to fill the left listbox (name="from[]" id="search")
$g_query1 = $conn->query('SELECT * FROM memberdetails WHERE mem_id NOT IN (SELECT mem_id FROM memberdetails WHERE mem_id in (select mem_id from classmember where class_id= '.$class.')) ORDER BY firstname, lastname ASC') or die(mysqli_error($conn));
// Finally Get list of memebers in selected class - used to fill right listbox (name="to[]" id="search_to")
$g_query2 = $conn->query('SELECT * FROM memberdetails WHERE mem_id IN (SELECT mem_id FROM memberdetails WHERE mem_id in (select mem_id from classmember where class_id= '.$class.')) ORDER BY firstname, lastname ASC') or die(mysqli_error($conn));
}
// Process after the submit button clicked
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// 1. delete existing records for the required class
// 2. now add all records from second listbox by exploding hidden text
header("location: home.php");
}
?>
The Form part of the body
Code:
<div class="row">
<form class="form-horizontal" method = "POST" action="<?PHP echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<div class="col-xs-5">
<h2 style="text-align:center">Available members</h2>
<select name="from[]" id="search" class="form-control" size="24" multiple="multiple">
<?php
// get array of all club members not in required class
while($g_fetch = $g_query1->fetch_array()){
echo "<option value = ".$g_fetch['mem_id'].">".$g_fetch['firstname'].' '.$g_fetch['lastname']." </option>";
}
?>
</select>
</div> <!-- col-xs-5 -->
<div class="col-xs-2">
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<button type="button" id="search_rightSelected" class="btn btn-info btn-block"><i class="glyphicon glyphicon-chevron-right"></i></button>
<button type="button" id="search_leftSelected" class="btn btn-info btn-block"><i class="glyphicon glyphicon-chevron-left"></i></button>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<button type="submit" name="save" id= "save" class="btn btn-primary btn-block"><span class = "glyphicon glyphicon-save"></span> Save Changes</button>
</div> <!-- col-xs-2 -->
<div class="col-xs-5">
<h2 style="text-align:center"><?php echo $class_name?> members</h2>
<select name="to[]" id="search_to" class="form-control" size="24" multiple="multiple">
<?php
// get initial array of members in class
while($g_fetch = $g_query2->fetch_array()){
echo "<option value = ".$g_fetch['mem_id'].">".$g_fetch['firstname'].' '.$g_fetch['lastname']." </option>";
}
?>
<input type="hidden" id="idlist" name="idlist">
</select> <!-- col-xs-5 -->
</div>
</form>
</div> <!-- row -->
Below the </body> tag is the following Javascript
Code:
<script type="text/javascript" src="../jquery/dist/jquery.min.js"></script>
<script type="text/javascript" src="../bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../js/multiselect.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#search').multiselect({
search: {
left: '<input type="text" name="q" class="form-control" placeholder="Search..." />',
right: '<input type="text" name="q" class="form-control" placeholder="Search..." />',
},
fireSearch: function(value) {
return value.length > 3;
}
});
});
</script>
What I have been trying to do is:
1) In the form include a hidden text input
2) Create an onselect script to extract all the IDs of the second selects options as comma separated variable that is then written to the hidden text.
3) In the $_POST of the PHP section:
Delete all existing records for the selected class
Explode the array in the Hidden input
For each mem_ID insert the class_id and mem_id into the table.
If I manually write the data to the hidden input I can get the PHP side to work ok. i.e. If I used value="1104,1328,787,1002" whthe Table would end up with records for those 4 values.
It's the onselect script and inserting it into the hidden input I am having problems with.
Cheers
Roger