K
K
Kozebojka2018-02-27 17:15:42
JavaScript
Kozebojka, 2018-02-27 17:15:42

Passing empty checkboxes and "not set" values?

There is a simple checkbox in the form, when filling (it doesn't matter whether it's Ajax or not), an empty value is not transmitted and the key is also ignored. In order not to complicate the task of checking on js or on the backend (many different forms and checkboxes), I add input hidden in front of it with the same name. Thus, if the checkbox is pressed, then it overrides hidden, and if it is not pressed, then hidden sets the default value.

<input type="hidden" name="set" val="N">
<input type="checkbox" name="set" val="Y">

It is important for me to know whether this checkbox was on the form at all or it was simply not clicked
Tell me what problems there may be with such an implementation and there may be other solutions

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2018-02-27
@Azperin

why does the checkbox have value Y ? there is transmitted on or off

if ($_POST['set'] === "on") {
 //чекбокс включен
} else {
 //выключен
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question