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