Jump to content

Problems with checkbox on form


bulrush

Recommended Posts

This is related to how PHP processes checkboxes in a form. I have a checkbox in a form called chkNewpart:

    
    $s='<tr><td>Model: <td><input type="text" name="txtModelnum" id="txtModelnum" value="'.$row['modelnum'].'" size="20" maxlength=15 />';
    $s.=' New part? <input type="checkbox" name="chkNewpart" value="'.$newpartvar.'" ';
    $s.='checked="';
    if ($newpartvar==1)
    	{
    	$s.='checked';
    	}
    	
    $s.='" />';
    $s.='</tr>';

 

In my db, the field that holds this value is a tinyint, and the default is 1, which stands for true. So when I display the checkbox, if the value of the field is 1, then the box should be checked. That part works.

 

The problem I have is when I uncheck the box and save the checkbox to a php variable, and then the database field.

            
$newpartvar=$_POST['chkNewpart'];
...
            $query = "UPDATE parts SET modelnum='".$modelvar."', ".
            "prodcat='".$prodcatvar."', ".
            "prodname='".$prodnamevar."', ".
            "prodsubname='".$prodsubnamevar."', ".
            "newflag=";
            if ($newpartvar==1)
            	{
            	$query.="1"; //True
            	}
            else {
            	$query.="0"; //False
            	}
            $query.=", ";
            $query.="updateuser='".$_SESSION['username']."', ".
            "updatedate=NOW() ".
            "WHERE partid=".$partidvar.";";

I get no errors but php doesn't seem to change the field value, when I look at it in PHP Admin.

 

How do I handle the values of checkboxes properly? I'd like 1 to be true and 0 to be false.

 

Link to comment
https://forums.phpfreaks.com/topic/209359-problems-with-checkbox-on-form/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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