How to submit 0 if checkbox is unchecked and submit 1 if checkbox is checked in HTML

后端 未结 3 1532
闹比i
闹比i 2020-12-09 08:40

How to submit value 1 if a checkbox in a checkbox array is checked, and submit 0 if it\'s unchecked? I tried this but no luck. I am trying to grab this array in a php array

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-09 09:00

    Simplest one, no javascript required, just put a hidden input before the checkbox:

    
    
    

    Inputs need to have the same name. If the checkbox is checked then value 1 will be submitted, otherwise value 0 from the hidden input.

    Your case javascript solution, no hidden inputs needed:

    
    
    

    PHP solution, no hidden inputs needed:

    
    
    

    EDIT: the javascript solution is not valid anymore as of jquery 1.6. Based on this, a more proper solution is the following:

    
    
    

提交回复
热议问题