I know I posted this code in another post, but since then I've decided that that post doesn't describe my problem. Here is the code:
function setSustainability( $pid, $psust, $conn){
if(isset($_POST['sustainability'.$pid ])){ // means the checkbox is checked
mysql_query(..change db value to 1);
}else { // the checkbox isn't checked
mysql_query(...change db value to 0);
}
}
<? 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; ?>
This looks to me like it should do exactly what I want it to do. It should let me use a checkbox to change a value to 1 if checked, 0 if unchecked, in a database. It does...but for only 1 form. It appears that the setSustainability() method is being called when the page loads instead of only when the "Save" buttons are clicked. (If I check a box, save it, and then reload the page, the values are correctly changed in the db table, and the checkbox is checked as it should be...but when I reload it again, then the values are set back to 0 and the box is unchecked). Note: I am not "refreshing" the page and therefore resending post data..I am just clicking a link to the page.
This makes absolutely no sense to me, I've given each form a different name using the id of the db item, so shouldn't the method only get called when a "Save" is clicked, and only for that button's form in particular?
Please help me out here!
Thanks