Here is the form code and the jQuery I will edit this answer in a minute with the separate PHP file to handle the DB query
<!-- Must include jQuery Library -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
// Lets build the Services form
${'The Form'} = '<form name="editServicesForm" id="editServicesForm" action="edit_services.php" method="POST">
<h2>Services</h2>';
// query the service table
$query = mysql_query('SELECT * FROM `services`');
while($row = mysql_fetch_assoc($query)){
${'The Form'} .= '<label><input type="checkbox" name="CheckboxGroup[]" value="'.$row['service_id'].'" id="CheckboxGroup_0" />'.$row['service_name'].'</label><br />';
}
${'The Form'} = '</form>';
// add the form on the page where you need with this
?>
<?= ${'The Form'}; ?>
<!-- The jQuery to do the magic -->
<script type="text/javascript">
// waits for the document to finish loading so all the elements are there to manipulate
$(document).ready(function() {
// your users id to reference the services in the database query
var businessID = "1" // replace this with the users id
// Do a basic post to and external php file
$.post('post.api.php', {'api': 'getServices', 'business_id': businessID}, function(resp){
// parse the response and check the boxes
var obj = $.parseJSON(resp);
// loop through the services returned as active (checked)
$.each(obj, function(i, value){
// Check the checkbox with the corresponding value
$('input[type="checkbox"][value="'+value.service+'"]').attr('checked', true);
});
});
});
</script>
Contents op post.api.php
<?php
// only access this if there is value for api being posted to it
if(isset($_POST['api'])){
// only access this if $_POST['api'] = getServices
if($_POST['api'] == 'getServices'){
// Create and array to store the data
${'Response'} = array();
// Get the users id
${'Business ID'} = $_POST['business_id']; // you should clean this to avoid sql injection
// Iterator
${'Iterator'} = 0;
// Query your database and return the values of the services that you stored during registration.
$sql = "SELECT `service_id_fk` FROM `business_services_offered` WHERE `business_id` = '".${'Business ID'}."'"; // your WHERE statement should include the user id sent here in ${'User ID'}
$query = mysql_query($sql);
// Do your while loop with your query results
while($row = mysql_fetch_assoc($query)){
// store our service value
${'Response'}[${'Iterator'}]['service'] = $row['service_id_fk'];
// increment our iterator
${'Iterator'}++;
}
// return the json to the posting file
echo json_encode(${'Response'});
}
exit;
}
?>