Jump to content

[SOLVED] checkbox works for "unchecked" but not "Checked"


Grant Holmes

Recommended Posts

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!

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.