Grant Holmes Posted December 21, 2007 Share Posted December 21, 2007 I have a "Status" field called Active that is a TinyINT. The only values will be ONE or ZERO designating on/off (or yes/no). At first I thought it would be easy to show/change the status (value) using a checkbox. So far, I'm easily showing the value in a check box: <?php $checked = $Active ? "checked" : ""; echo "<input type=\"checkbox\" name=\"foo\" $checked>"; if ($foo == "checked") $value = 1; else $value = 0; ?> The user will click an "edit" link to change any part of the record (like name, addr., etc) as well as changing status. I cannot figure out on my edit page how to modify the above to not only show the current status, but change it. I'm open to radio buttons a On/Off pull down list (that would show correct status upon load) or another simple answer. I could just bring the box up as INPUT and let the user change 1 to 0, etc. but we all know how that will end up. Help? Quote Link to comment https://forums.phpfreaks.com/topic/82723-simple-way-to-toggle-onoff-field/ Share on other sites More sharing options...
revraz Posted December 21, 2007 Share Posted December 21, 2007 This is how I toggle my online booking Paid status for a person if ($row2[0] == "Yes") { $paid2 = "No"; } else { $paid2 = "Yes"; } So when I click on the word Yes, it changes to No. If I click on No, it changes to Yes. Quote Link to comment https://forums.phpfreaks.com/topic/82723-simple-way-to-toggle-onoff-field/#findComment-420730 Share on other sites More sharing options...
Grant Holmes Posted December 21, 2007 Author Share Posted December 21, 2007 revraz, Thanks for the quick reply. I replaced my code with yours. However, I see nothing. <?php if ($num2[0] == "Yes") { $Active = "No"; } else { $Active = "Yes"; } ?> I changed your $paid2 to my $Active Quote Link to comment https://forums.phpfreaks.com/topic/82723-simple-way-to-toggle-onoff-field/#findComment-420743 Share on other sites More sharing options...
ChadNomad Posted December 21, 2007 Share Posted December 21, 2007 Are you trying to check a checkbox? If so... $checked == $Active ? "checked='checked'" : ""; This always return true as your making $checked equal to $Active and not "is equal to?". To compare use == $checked = $Active ? "checked" : ""; Quote Link to comment https://forums.phpfreaks.com/topic/82723-simple-way-to-toggle-onoff-field/#findComment-420751 Share on other sites More sharing options...
Grant Holmes Posted December 21, 2007 Author Share Posted December 21, 2007 In this post, I've not admitted my noob status. I'm so sorry, but I'm not sure where to put the snippets. I can show the status on one screen. I need to change it on another. So if it is unchecked, I need to return a zero to the table, on checked the value needs to be one. I'm not sure if the solutions offered are to replace mine or augment it. After the check/uncheck, the user will hit an update button. Quote Link to comment https://forums.phpfreaks.com/topic/82723-simple-way-to-toggle-onoff-field/#findComment-420757 Share on other sites More sharing options...
ChadNomad Posted December 21, 2007 Share Posted December 21, 2007 Using your code... <?php $checked = $_POST["foo"]; if ($checked == "checked") { $value = 0; // It's checked so make it 0 } else { $value = 1; // It isn't checked, value is 1 } echo "The value is ".$value; ?> HTML: <form method="post"> <input type="checkbox" name="foo" /> <input type="submit" name="submit" /> </form> Why are you using things such as ternary conditions as a noob? Keep it simple and easily readable! I haven't tried the code above but it should work. Quote Link to comment https://forums.phpfreaks.com/topic/82723-simple-way-to-toggle-onoff-field/#findComment-420763 Share on other sites More sharing options...
Grant Holmes Posted December 21, 2007 Author Share Posted December 21, 2007 Chad, thank you for trying, but I'm getting the same result (The value is 1) no matter if the field is checked or not. And checking it and hitting update changes nothing. Also, wouldn't I have to reflect my field name "Active" in the code? Quote Link to comment https://forums.phpfreaks.com/topic/82723-simple-way-to-toggle-onoff-field/#findComment-420770 Share on other sites More sharing options...
emehrkay Posted December 21, 2007 Share Posted December 21, 2007 that is because you need to add the value attribute to your checkbox, it is basic html <input type="checkbox" name="foo" value="1" /> //processing if($_POST['foo']){ //do something } Quote Link to comment https://forums.phpfreaks.com/topic/82723-simple-way-to-toggle-onoff-field/#findComment-420771 Share on other sites More sharing options...
Grant Holmes Posted December 21, 2007 Author Share Posted December 21, 2007 Thanks emehrkay, However, the reason I admitted to Newbie status above is that when you very proficient people provide snippets like you did, doesn't help me as I have no clue what to do with it. Even your helpful answer means nothing as I don't know where to put it. Sigh. Here's what I have that is showing the value, but doesn't allow me to change anything. Above this point I've already selected the record on the page and established the table it is in: <TD> <!-- // Set the variable $foo from the database or from form data. --> <?php $checked = $Active ? "checked" : ""; echo "<input type=\"checkbox\" name=\"foo\" $checked>"; ?> </TD> <TD><CENTER><input type="submit" name="submit" value="Update"> Can someone please show me the code again with correct editing? I really appreciate the help. Quote Link to comment https://forums.phpfreaks.com/topic/82723-simple-way-to-toggle-onoff-field/#findComment-420784 Share on other sites More sharing options...
emehrkay Posted December 21, 2007 Share Posted December 21, 2007 so $Active is set to the value from the Database, being one or zero? $checked = ($Active) ? 'checked="checked"' : ''; //this is a ternary - (condition) ? value if true : value if false; echo "<input type=\"checkbox\" name=\"foo\" value=\"". $Active ."\" ". $checked .">"; try that I dont know what you mean by "change anything" the only change that will happen when the checkbox is checked or not is when you process the form unless you do some JavaScripting Quote Link to comment https://forums.phpfreaks.com/topic/82723-simple-way-to-toggle-onoff-field/#findComment-420788 Share on other sites More sharing options...
Grant Holmes Posted December 21, 2007 Author Share Posted December 21, 2007 We're getting closer! At least now, when I uncheck the button, it passes that value (ZERO), but will not pass the "Check" or ONE. My page didn't like the () around my field, so I removed them. And to answer your question, yes: pass the value "1" if checked; "0" if unchecked. Here's what I have now: <?php $checked = $Active ? 'checked="checked"' :''; //this is a ternary - (condition) ? value if true : value if false; echo "<input type=\"checkbox\" name=\"foo\" value=\"". $Active ."\" ". $checked .">"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/82723-simple-way-to-toggle-onoff-field/#findComment-420795 Share on other sites More sharing options...
emehrkay Posted December 21, 2007 Share Posted December 21, 2007 I am confused. What exactly are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/82723-simple-way-to-toggle-onoff-field/#findComment-420799 Share on other sites More sharing options...
Grant Holmes Posted December 21, 2007 Author Share Posted December 21, 2007 pass the value "1" if checked; "0" if unchecked. It would appear it's passing the "0", but not the "1" That help? Quote Link to comment https://forums.phpfreaks.com/topic/82723-simple-way-to-toggle-onoff-field/#findComment-420801 Share on other sites More sharing options...
Grant Holmes Posted December 22, 2007 Author Share Posted December 22, 2007 This morning I am still stuck halfway. I have ONE checkbox in my example for ONE field. Thanks to all of your help, I've modified my code and "most" of it works: 1) I'm pulling the value of a TinyINT field (containing a 1 or 0) from MySQL- That Works 2) I'm displaying the correct DB value in a checkbox- Works ---If the field value=1 it's checked and if the field value=0 it's unchecked. 3) You click an edit link that takes you to another page that allows you to update any field in that record- Works 4) On the edit page I can update any INPUT field upon SUBMIT- Works 5) If the single checkbox is checked and I uncheck it-(changing the 1 to a 0)-- upon SUBMIT, That Works. Here's the issue. During an edit, IF the checkbox (or stored value) =0... I change any input field and I check the box (wanting to change the 0 to a 1), all the input fields are updated, But nothing is returned to that (Active) field-the value remains zero. Here's where I am Code-wise: <?php $checked = $Active ? 'checked="checked"' :''; //this is a ternary - (condition) ? value if true : value if false; echo "Active? <input type=\"checkbox\" name=\"foo\" value=\"". $Active ."\" ". $checked .">"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/82723-simple-way-to-toggle-onoff-field/#findComment-421143 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.