<!-- See http:kinghorsemedia.ca/bna/ Click on Trailer Configurator, pick a model and click on Accessories
This is html that is loaded when tab_3 is clicked -->
<div id="tab_3">
<h3><?php htmlout($model_name); ?> – Accessories</h3>
<h4>Select Optional Accessories by clicking the boxes. </h4>
<p>Click on underlined text to view image.</p>
<form enctype="multipart/form-data" id="acc_form" action="null" method="post">
<?php
$acc_type = '';
foreach ($accessories as $accessory): ?>
<div>
<?php
if ($acc_type != $accessory['type'])
{
if ($acc_type != '')
{ ?>
<br/><h5><?php htmlout(substr($accessory['type'], 2)); ?></h5>
<?php
}else{ ?>
<h5><?php htmlout(substr($accessory['type'], 2)); ?></h5>
<?php
}
$acc_type = $accessory['type'];
}
if ($empty_selected_acc)
{ ?>
<input type="checkbox" name="cfg_accessories[]" onclick="checkClick"
id="accessory<?php htmlout($accessory['id']); ?>"
value="<?php htmlout($accessory['id']); ?>" class="<?php htmlout($accessory['chassis']);?>"
<?php
// if accessory id is saved in Session variable selected_acc, indicate checked
if ($accessory['selected'])
{
echo ' checked="checked"';
}
?>>
<?php
} else
{ ?>
<input type="checkbox" name="cfg_accessories[]" onclick="checkClick"
id="accessory<?php htmlout($accessory['id']); ?>"
value="<?php htmlout($accessory['id']); ?>" class="<?php htmlout($accessory['chassis']);?>"
<?php
// if accessory id is saved in Session variable selected_acc, indicate checked
if ($accessory['selected'] || in_array($accessory['id'], $selected_acc))
{
echo ' checked="checked"';
}
?>>
<?php
} ?>
<label for="accessory<?php htmlout($accessory['id']);?>">
<?php
if ($accessory['acc_photo'] != '')
{ ?>
<a target="_blank" rel="<?php htmlout($accessory['type']); ?>" href="../img/accessories/<?php print $accessory['acc_photo'] ?>" title="<?php htmlout($accessory['description']); ?>" ><?php htmlout($accessory['description']); ?></a>
<?php
}else{
htmlout($accessory['description']);
} ?>
</label>
</div>
<?php endforeach; ?>
<input type="hidden" value="<?php htmlout($model_id); ?>" id="model_id" name="model_id"/>
</fieldset>
</form>
<ul class="model_desc">
<li><input class="accessories_back button" type="submit" value="« Back" /></li>
<li><input class="save_accessories button" type="submit" name="action" value="Next »" /></li>
</ul>
</div>
$(document).on('change', ':checkbox[name="cfg_accessories[]"]', (function(e) {
//Each checkbox click invokes save_accessories.php to store aray of selected accessories
var data = [];
var session_id = $('#session_id').val();
var not_empty = false;
if ($('input[type=checkbox]').is(':checked'))
{
not_empty = true;
data = $(":checkbox:checked").map(function(i,n) {
return $(n).val();
}).get();
$.post('save_accessories.php', {'sid': session_id, 'saved_accessories[]': data, 'not_empty': not_empty })
}
else
// All checkboxes are de-selected, so empty the SESSION['selected_acc'] array
{
$.post('save_accessories.php', {'sid': session_id, 'saved_accessories[]': data, 'not_empty': not_empty });
}
e.preventDefault();
}) )
<?php
//This save_accessories.php
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/bna/helpers.inc.php';
session_start();
session_id($_POST['sid']);
if (!isset($_SESSION['selected_acc']))
{
$_SESSION['selected_acc'] = array();
}
//Empty selected_acc Session variable
unset($_SESSION['selected_acc']);
$_SESSION['selected_acc'] = array();
$selected_acc = array();
$not_empty = $_POST['not_empty'];
// saved_parts is generated in configurator.js
if ($not_empty)
{
foreach ($_POST['saved_accessories'] as $value)
{
// $_SESSION['selected_acc'][] = $value;
$selected_acc[] = $value;
}
$_SESSION['selected_acc'] = $selected_acc;
}
else
{
$_SESSION['selected_acc'] = '';
}
//var_dump($selected_acc);
?>