How to read if a checkbox is checked in PHP?

后端 未结 18 1220
刺人心
刺人心 2020-11-22 07:54

How to read if a checkbox is checked in PHP?

18条回答
  •  萌比男神i
    2020-11-22 08:48

    I've been using this trick for several years and it works perfectly without any problem for checked/unchecked checkbox status while using with PHP and Database.

    HTML Code: (for Add Page)

    
    

    Hint: remove "checkbox" if you want to show it as unchecked by default

    HTML Code: (for Edit Page)

    >
    

    PHP Code: (use for Add/Edit pages)

    $status = $_POST['status'];
    if ($status == 1) {
    $status = 1;
    } else {
    $status = 0;
    }
    

    Hint: There will always be empty value unless user checked it. So, we already have PHP code to catch it else keep the value to 0. Then, simply use the $status variable for database.

提交回复
热议问题