Using an HTML checkbox to put 1 or 0 into a MySQL table

后端 未结 5 1470
离开以前
离开以前 2020-12-15 22:23

I am using a simple HTML checkbox in a form to put a 1 for checked and a 0 for unchecked in a field called \"subcheck\" in a MySQL table.

Does the checkbox default t

相关标签:
5条回答
  • 2020-12-15 22:50

    If the checkbox isn't ticked then nothing is sent to the server. Instead, you can provide a default value with:

    $subcheck = (isset($_POST['subcheck'])) ? 1 : 0;
    
    0 讨论(0)
  • 2020-12-15 22:59

    If Checkbox is unchecked then $subcheck is not submited at all.

    In PHP you should write:

    if !(isset($_POST['subcheck']))
      $subcheck = 0;
    
    mysql_query("INSERT INTO submission VALUES (NULL, '$uid', '$title', '$slug', '$cleanurl', '$displayurl', NULL, '$subcheck')");
    
    0 讨论(0)
  • 2020-12-15 23:01

    To be sure that always 1 or 0 is sent, you can insert an input hidden with the same name of the checkbox in the html:

    //The input hidden
    <input type="hidden" name="subcheck" value="0" />
    
    //The checkbox
    <input type="checkbox" name="subcheck" value="1" /> 
    

    This way, you don't need to check in server side if the textbox is set or not ;)

    0 讨论(0)
  • 2020-12-15 23:09

    This can be used as follows

    on form page

        <input type="checkbox" name="status" value="1" >
    

    on processing page

        $status = (isset($_REQUEST['status']));
        if ($status == 1 )
          {
            $status = 1;
          }
        else
         {
           $status = 0;
         }
       echo $status;
    
    0 讨论(0)
  • 2020-12-15 23:13

    u should try if statement or ternary condition statement.. if(isset($_POST['subcheck'])) $subcheck = 1; else $subcheck = 0;

    or u can try $subcheck = (isset($_POST['subcheck'])) ? 1 : 0;

    and in database u should use enum type for subcheck

    0 讨论(0)
提交回复
热议问题