I am fairly new to Jquery and javascript and have jumped in head first and am a bit stuck.
I have a page where clicking a button opens a small overlay via an AJAX call that then gives a whole bunch of checkboxes for users to choose what tags they want to associate with the keywords. I'm then trying to process the checkboxes via another ajax call to process. So far everything looks okay, except I can't get the data from which checkboxes are ticked and which aren't through the form.
I'm using jquery, PHP & MySQL combo; see the below snippet.
<form method="post" >
<h1>Categories</h1>
<fieldset id="edit_box">
<legend>Choose Tags</legend>
<?php
// $tags is an array with all the original values from the database, so a list of available tags and if they are set for each category
foreach($tags as $value){
if(isset($value['set']) && $value['set'] ==1) $set = "checked = 'checked'"; else $set = "";
echo'<p class="floatlist">
<input type="checkbox" name="'.$value['id'].'" id="'.$value['id'].'" '.$set.' />
<label for="'.$value['id'].'" class="inline">'.$value['name'].'</label>
</p>';
}
?>
<p><button type="button" id="update_tags">Save Changes</button></p>
</fieldset>
</form>
<script type="text/javascript">
$("#update_tags").click(function(){
var data = new Array();
$('#edit_box input').each(function() {
var tagid = $(this).attr('id');
if($('#'+tagid ).attr('checked')){data[tagid] = '1';} else {data[tagid] = 0;}
});
$.post("inc/process_tags.php", { page: "update_tags", categories: data }, function(data){
var output = data;
$('#edit_box').html(output);
});
});
</script>
At the moment, I get an array out of this that corresponds with the original options (so if item 1 3 & 5 were checked, they show correctly) however if I change the variables before I submit it, (changing it so item 1 2 & 5 were checked) it still shows the output as the original items (1 3 & 5) being checked. What am I missing here?
I don't unfortunately have access to a live example I can give you.
Thanks in advance!