palacios Posted May 17, 2010 Share Posted May 17, 2010 Hi, I'm trying to use check-boxes in a webpage to update the information kept in a database. I managed to understand how it works with text but with checkbox i dont even know if it possible. <form id="form1" name="form1" method="post" action=""> <p> <label for="name">name:</label> <input name="name" type="text" class="widebox" id="name" value="<?php echo htmlentities($row['name']); ?>" /> <!-- so far so good --> </p> <p> <label for="air_conditioning">air-conditioning</label> <input name="air_conditioning" value="air_conditioning" type="checkbox" name="air_conditioning" id="air_conditioning" <?php // i came up with this code but it doenst work, the display from the db comes correct but if i want to update using the checkbox, nothing really happens. $yesorno = ($row['air_conditioning']); $option = $yesorno == 'Y' ? "checked" : "unchecked"; echo ($option); ?> /> </p> <p> <input type="submit" name="update" value="save changes!" /> <input name="id" type="hidden" value="<?php echo $row['id']; ?>" /> </p> </form> please advice and thank you in advance, giulio Quote Link to comment https://forums.phpfreaks.com/topic/202063-update-record-using-checkbox/ Share on other sites More sharing options...
siric Posted May 17, 2010 Share Posted May 17, 2010 I did a simple test by setting $row['air_conditioning'] to both Y and N and it works. Quote Link to comment https://forums.phpfreaks.com/topic/202063-update-record-using-checkbox/#findComment-1059647 Share on other sites More sharing options...
Psycho Posted May 17, 2010 Share Posted May 17, 2010 What are the possible values for the 'air_conditioning' field from the DB? It SHOULD be 1/0 for true/false, not Y/N. Anyway, there were some other minor errors. This should work, but change the 'Y' to '1' if you get around to fixing the DB values. <?php $name = htmlentities($row['name']); $checked = ($row['air_conditioning']=='Y') ? 'checked="checked"' : ''; ?> <form id="form1" name="form1" method="post" action=""> <p> <label for="name">name:</label> <input name="name" type="text" class="widebox" id="name" value="<?php echo $name; ?>" /> </p> <p> <label for="air_conditioning">air-conditioning</label> <input name="air_conditioning" value="true" type="checkbox" id="air_conditioning"<?php echo $checked; ?> /> </p> <p> <input type="submit" name="update" value="save changes!" /> <input name="id" type="hidden" value="<?php echo $row['id']; ?>" /> </p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/202063-update-record-using-checkbox/#findComment-1059669 Share on other sites More sharing options...
palacios Posted May 18, 2010 Author Share Posted May 18, 2010 Thank you all for your help. I think I have some errors in the code somewhere else, if I use your revised code I have on the db some other characters like 'm' or 'c', for example: linen_change: l maid_service: m cctv: c actually -now that i'm writing it - looks like the db is filling in the cells with the firs letter of the field name... strange... my questions now are: * is it really necessary to grasp the value of the database and link it to a variable (as shown in mjdamao post)? does it bring any advantage? * i understand the suggestion to use 1-0 instaed of Y-N in the database, but shall i change also the field type? at the moment is set up as 'char' with lenght '1', I dont see any boolean option for the field type or am I missing something? thanks a lot again giulio Quote Link to comment https://forums.phpfreaks.com/topic/202063-update-record-using-checkbox/#findComment-1060185 Share on other sites More sharing options...
Psycho Posted May 18, 2010 Share Posted May 18, 2010 my questions now are: * is it really necessary to grasp the value of the database and link it to a variable (as shown in mjdamao post)? does it bring any advantage? If you don't compare the value from the database how would you make the determination of whether to show the checkbox as checked or not to indicate the currently saved value? * i understand the suggestion to use 1-0 instaed of Y-N in the database, but shall i change also the field type? at the moment is set up as 'char' with lenght '1', I dont see any boolean option for the field type or am I missing something? You should use a field type of tinyint Quote Link to comment https://forums.phpfreaks.com/topic/202063-update-record-using-checkbox/#findComment-1060239 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.