ebolisa Posted March 13, 2020 Share Posted March 13, 2020 Hi, I'm trying to check a box in a form based on the $status variable read from a json file. If it's a 1 then the checkbox is checked, otherwise not. If I check/uncheck it, then the $status is written back to the json file. It's a rather simple code but I just cannot get it right. I appreciate any help. TIA <div class="field"> <?php if ($status == 1){ $checked = "checked"; } echo '<input name="ck1" type="checkbox" value='.$status.' '.$checked.' >'; ?> <label for="ck1">Active</label> <!-- <input name='ck1' type='checkbox' <? if $status : "checked" ?> value = "<?=$status ?>" /> --> </div> Quote Link to comment https://forums.phpfreaks.com/topic/310285-ckbox-cked-based-on-var/ Share on other sites More sharing options...
requinix Posted March 13, 2020 Share Posted March 13, 2020 The code could use some improvement but it should work. Have you done a View Source of the page to make sure the HTML being outputted is correct? Does "cannot get it right" mean anything more than just the checkbox remaining unchecked? Quote Link to comment https://forums.phpfreaks.com/topic/310285-ckbox-cked-based-on-var/#findComment-1575413 Share on other sites More sharing options...
ebolisa Posted March 13, 2020 Author Share Posted March 13, 2020 (edited) Yes, remains unchecked. It always sends out 0 instead of 1 when checked. I noticed that the double quotes remain around the word checked. Is I remove them, it works. Edited March 13, 2020 by ebolisa Quote Link to comment https://forums.phpfreaks.com/topic/310285-ckbox-cked-based-on-var/#findComment-1575415 Share on other sites More sharing options...
requinix Posted March 13, 2020 Share Posted March 13, 2020 ... What is the HTML outputted, both when it didn't work and after your change that made it work? Quote Link to comment https://forums.phpfreaks.com/topic/310285-ckbox-cked-based-on-var/#findComment-1575417 Share on other sites More sharing options...
ebolisa Posted March 13, 2020 Author Share Posted March 13, 2020 Modified a bit the code but still no avail... <?php if ($status == 1){ echo '<input name="ck1" type="checkbox" value='.$status.' checked >'; } else { echo '<input name="ck1" type="checkbox" value='.$status.' >'; } ?> The html output is this... <div class="field"> <label for="ck1">Activa</label> <input name="ck1" type="checkbox" value=0 > </div> Quote Link to comment https://forums.phpfreaks.com/topic/310285-ckbox-cked-based-on-var/#findComment-1575421 Share on other sites More sharing options...
ginerjm Posted March 13, 2020 Share Posted March 13, 2020 (edited) What that html will do is return a 0 when the box is checked. It returns nothing at all if it is not checked. In fact the $_POST array will not have an element named 'ck1' in it unless the box is checked. As for returning a different value, perhaps you should be using a pair of radio buttons instead of a checkbox. Edited March 13, 2020 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/310285-ckbox-cked-based-on-var/#findComment-1575423 Share on other sites More sharing options...
ebolisa Posted March 13, 2020 Author Share Posted March 13, 2020 Ok, so how do I include an If statement within a form code? Quote Link to comment https://forums.phpfreaks.com/topic/310285-ckbox-cked-based-on-var/#findComment-1575425 Share on other sites More sharing options...
ginerjm Posted March 13, 2020 Share Posted March 13, 2020 You don't. An if statement is not html. Form code is. ?? What are you talking about? Quote Link to comment https://forums.phpfreaks.com/topic/310285-ckbox-cked-based-on-var/#findComment-1575427 Share on other sites More sharing options...
ebolisa Posted March 13, 2020 Author Share Posted March 13, 2020 In my first post you can see the php If statement within the form code. Quote Link to comment https://forums.phpfreaks.com/topic/310285-ckbox-cked-based-on-var/#findComment-1575428 Share on other sites More sharing options...
ebolisa Posted March 13, 2020 Author Share Posted March 13, 2020 (edited) Ok, found the solution. Thank you. <div class="field"> <label for="ck1">Activa</label> <input name="ck1" type="checkbox" value = 1 <?php echo ($status=="1" ? 'checked' : '');?> /> </div> Edited March 13, 2020 by ebolisa Quote Link to comment https://forums.phpfreaks.com/topic/310285-ckbox-cked-based-on-var/#findComment-1575429 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.