Grant Holmes Posted December 23, 2007 Share Posted December 23, 2007 I have an edit form that is working perfectly for editing the input fields in the form. This form also contains ONE checkbox field which works if I'm UNchecking it. "Works" as defined by changing the database from a "1" to a "0". However, if the box is NOT checked, when I check the box and submit (This should change the value from "0" to "1", it does nothing. I'm not sure how to edit the code I'm using to make it work. The field I'm trying to update is named "Active" and I set the field to "0" elsewhere. I need this code to allow me to change the "Active" field from "1" to a "0" OR "0" to "1" and submit. <?php $checked = $Active ? 'checked="checked"' :''; //this is a ternary - (condition) ? value if true : value if false; echo "Active? <input type=\"checkbox\" name=\"Active\" value=\"". $Active ."\" ". $checked .">"; ?> The rest of the input fields work fine regardless of the above. Can someone help me put this issue to bed?? Thanks in advance! Quote Link to comment Share on other sites More sharing options...
papaface Posted December 23, 2007 Share Posted December 23, 2007 Sometimes checked="checked" works. Sometimes its just checked depending on your browser. Also instead of escaping all your " 's just use echo 'some text <a href="http://www.domain.com">text</a>'; Quote Link to comment Share on other sites More sharing options...
Grant Holmes Posted December 23, 2007 Author Share Posted December 23, 2007 Papa.... I'm using FireFox, I changed the line to; <?php $checked = $Active ? 'checked' :''; //this .... with no change in how it works. Also, I tested both versions in FF and IE. Same results. Quote Link to comment Share on other sites More sharing options...
papaface Posted December 23, 2007 Share Posted December 23, 2007 Are you sure checked="checked" is being used in the input tag? Quote Link to comment Share on other sites More sharing options...
Grant Holmes Posted December 23, 2007 Author Share Posted December 23, 2007 No, I'm not sure- how could I tell? I thought I'd taken care of that in the original statement?? I'm not highly skilled on this, so am not sure. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted December 23, 2007 Share Posted December 23, 2007 Where/when is the above code executed from? I guess this when the data being pulled from the database to populate the form. Web browsers do not submit checkboxes if they where not checked when the form was submitted. You'll only be able to retrieve the value from the checkbox if it was checked. So you cannot update the database based on the checkbox value (checked or no checked). Instead what you should do is see whether the checkbox variable exists, eg $_POST['Active'] and then assign $Active to 0 or 1 weather or not the checkbox variable exists and update the database that way, like so: $Active = (isset($_POST['active'])) ? $_POST['active'] : 0; Quote Link to comment Share on other sites More sharing options...
juapo2 Posted December 24, 2007 Share Posted December 24, 2007 As you can see, where it has the //this is a ternary - (condition) ? value if true : value if false; Means you have set it to true or false, depending if you want it checked or unchecked Quote Link to comment Share on other sites More sharing options...
juapo2 Posted December 24, 2007 Share Posted December 24, 2007 And if that doenst work, you can use "?" (without the quotes) if it is true Use ":" (without the quotes) if it is false Try the first thing i said, and if it doesn´t work, try this... Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted December 24, 2007 Share Posted December 24, 2007 As you can see, where it has the //this is a ternary - (condition) ? value if true : value if false; Means you have set it to true or false, depending if you want it checked or unchecked You couldn't be more off-base here. That comment was just a note on the syntax and meaning of the ternary operator's operands. Quote Link to comment Share on other sites More sharing options...
juapo2 Posted December 24, 2007 Share Posted December 24, 2007 I didnt understand what you told me man..:S Quote Link to comment Share on other sites More sharing options...
Grant Holmes Posted December 24, 2007 Author Share Posted December 24, 2007 Thank you all for the spirited interchange while I watched the Cleveland Browns throw away their season, but I digress... Yes, this EDIT page is populated from a table. The table is populated from another form where I use a hidden field to put a "0" in the Active field. So the user gets an email to check the entries. They log in and see all the records where I do show the checkbox on/off (1/0) FROM the Table. They click an EDIT link on the page where I pass the record number forward- again, what I have is showing the on/off (1/0) from the Table accurately. This code is within the area where I allow editing of the other INPUT fields (again, all updating accurately). WildTeen88; I tried plopping your snippet in, but prolly didn't put it in the right spot, so it didn't work. Here's the whole "edit" loop, including WildTeen88's code: <? php $i=0; while ($i < $num) { $Active=mysql_result($result,$i,"Active"); $Contact_Info_FirstName=mysql_result($result,$i,"Contact_Info_FirstName"); $Contact_Info_LastName=mysql_result($result,$i,"Contact_Info_LastName"); $Contact_Info_City=mysql_result($result,$i,"Contact_Info_City"); $Contact_Info_State=mysql_result($result,$i,"Contact_Info_State"); $Contact_Info_ZipCode=mysql_result($result,$i,"Contact_Info_ZipCode"); $Contact_Info_Country=mysql_result($result,$i,"Contact_Info_Country"); $id=mysql_result($result,$i,"id"); ?> <form method="post"> <input type="hidden" name="ud_id" value="<? echo $id; ?>"> <tr> <td valign="top"><STRONG><input type="text" name="ud_Contact_Info_FirstName" value="<? echo $Contact_Info_FirstName; ?>"><input type="text" name="ud_Contact_Info_LastName" value="<? echo $Contact_Info_LastName; ?>"></STRONG><BR> <input type="text" name="ud_Contact_Info_City" value="<? echo $Contact_Info_City; ?>">, <input type="text" name="ud_Contact_Info_State" value="<? echo $Contact_Info_State; ?>"> <input type="text" name="ud_Contact_Info_ZipCode" value="<? echo $Contact_Info_ZipCode; ?>"> <input type="text" name="ud_Contact_Info_Country" value="<? echo $Contact_Info_Country; ?>"> </td> <TD> <?php $checked = $Active ? 'checked' :''; //this is a ternary - (condition) ? value if true : value if false; echo "Active? <input type=\"checkbox\" name=\"Active\" value=\"". $Active ."\" ". $checked .">"; //WildTeen88 $Active = (isset($_POST['active'])) ? $_POST['active'] : 0; ?> </TD> <TD><CENTER><input type="submit" name="submit" value="Update"> <BR><BR><input type=button value="Cancel" onClick="history.go(-1)"><BR><BR> <input type="submit" name="submit" value="Delete" onclick="return confirm('Are you sure you want to delete this record? This cannot be undone!')"></CENTER> </form></TD> </TR> <? ++$i; } echo "</table><BR>"; ?> As I said above, I'm learning all this (rather painfully). If you have a solutions (Like WildTeen88), please tell me EXACTLY where to put it? Otherwise, I'll just be guessing and waste more time. Quote Link to comment Share on other sites More sharing options...
Barand Posted December 26, 2007 Share Posted December 26, 2007 A couple of problems with this <?php $checked = $Active ? 'checked' :''; //this is a ternary - (condition) ? value if true : value if false; echo "Active? <input type=\"checkbox\" name=\"Active\" value=\"". $Active ."\" ". $checked .">"; //WildTeen88 $Active = (isset($_POST['active'])) ? $_POST['active'] : 0; ?> 1 ) you are checking the value of $Active before you set it's value with Wildteen's code 2 ) the value of the checkbox should always be "1" but set as checked if Active is 1. So <?php $Active = (isset($_POST['Active'])) ? $_POST['Active'] : 0; $checked = $Active ? 'checked' :''; echo "<input type='checkbox'' name='Active'' value='1' $checked>"; ?> Quote Link to comment Share on other sites More sharing options...
Grant Holmes Posted December 26, 2007 Author Share Posted December 26, 2007 Barand, Thank you so much for the response!!! I replaced "WildTeens" code with yours. Now the current status of the box does not show, nor does checking/unchecking it do anything. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 26, 2007 Share Posted December 26, 2007 Maybe try it without the extra single quotes and using $checked as the Value. echo "<input type='checkbox' name='Active' value=$checked>"; Quote Link to comment Share on other sites More sharing options...
Barand Posted December 26, 2007 Share Posted December 26, 2007 Sorry, just re-read your code <?php $checked = $Active ? 'checked' :''; echo "<input type='checkbox'' name='Active'' value='1' $checked>"; ?> When you process the posted form data, use Wildteens code $Active = (isset($_POST['Active'])) ? $_POST['Active'] : 0; before you update the database table. Quote Link to comment Share on other sites More sharing options...
Grant Holmes Posted December 26, 2007 Author Share Posted December 26, 2007 Here's what I have now... <TD> <?php $checked = $Active ? 'checked' :''; echo "<input type='checkbox'' name='Active'' value='1' $checked>"; ?> </TD> <TD> <?php $Active = (isset($_POST['Active'])) ? $_POST['Active'] : 0; ?> <CENTER><input type="submit" name="submit" value="Update"> Is that Right??? I'm seeing no difference in behavior with it like this. Thanks gang. Keep trying please! Quote Link to comment Share on other sites More sharing options...
Grant Holmes Posted December 26, 2007 Author Share Posted December 26, 2007 Fixed. The change that enabled it to work was changing: <?php $checked = $Active ? 'checked' :''; echo "<input type='checkbox'' name='Active'' value='1' $checked>"; ?> To: <?php $checked = $Active ? 'checked' :''; echo "<input type='checkbox'' name='ud_Active'' value='1' $checked>"; ?> Thanks to all for chipping in... Quote Link to comment 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.