* I no longer believe that this is my problem, please see my new post *
Hi there, I'm trying to use checkboxes to set database values. If the box is checked, then the value in the db should be 1, if not then it should be 0. So far, I have been able to have the checkbox display checked/unchecked depending on the db value correctly. The problem I am having is that isset($_POST['checkbox'] ) only returns true if the checkbox is checked? here is my code:
<? while ( $p = mysql_fetch_assoc($res) ): ?>
<form method="POST" name="sustForm<?=$pid?>">
<label for="sustainability<?=$pid?>">Sustainability:</label>
<input type="checkbox" name="sustainability<?=$pid?>" <?php if ($p['sustainability'] == 1) echo 'checked="true"'; ?> /> (reload page from sidebar to update checkbox)
<input type="submit" name="submit_sust<?=$pid?>" value="Save" onClick=<?=setSustainability($pid, $p['sustainability'], $conn); ?> />
</form>
<? endwhile; ?>
function setSustainability( $pid, $psust, $conn){
if(isset($_POST['sustainability'.$pid ])){
if($psust == 0){
mysql_query(...change it to 1...);
}
}else{
if ($psust == 1 ) {
mysql_query(...change it to 0...);
}
}
}
So this code works fine when trying to check an individual checkbox, the problem comes in with the fact that I have a bunch of the same form basically... I need the form to display for each profile in the database, the checkbox is to change an attribute for that profile.
I think that if I was able to do something like:
if ('the checkbox is checked' && 'db value is 0'){
'change db value to 1'
}else if ('the checkbox is not checked' && 'db value is 1') {
'change db value to 0'
}
I can't figure out how to check if each checkbox is checked or not because of the fact that I need to have different names for each one, and to do that I've used the Id's of the profiles.
Does anyone know how I would be able to do this?
Thanks a lot!