The guys have such a question - is there a form with a lot of checkboxes, so how do you tell a php file to send the form if at least ONE of the checkboxes is pressed?

<tr> <td style="text-align: left;"> <h1 class="normal hairline ">Пн</td> <td><input type="checkbox" name="ПонедельникУтро" value="да"></td> <td><input type="checkbox" name="ПонедельникДень" value="да"></td> <td><input type="checkbox" name="ПонедельникВечер" value="да"></td> <td><input type="checkbox" name="ПонедельникНочь" value="да"></td> </tr> <?php { $to = 'godofwar9325@gmail.com'; $subject = 'Обратный звонок'; $message = ' <html> <head> <title>'.$subject.'</title> </head> <body> <p><b>ПонедельникУтро:</b> '.$_POST['ПонедельникУтро'].'</p> <p><b>ПонедельникДень:</b> '.$_POST['ПонедельникДень'].'</p> <p><b>ПонедельникВечер:</b> '.$_POST['ПонедельникВечер'].'</p> <p><b>ПонедельникНочь:</b> '.$_POST['ПонедельникНочь'].'</p> </body> </html>'; $headers = "Content-type: text/html; charset=utf-8 \r\n"; //Кодировка письма $headers .= "From: Отправитель <uber-key.ru>\r\n"; //Наименование и почта отправителя mail($to, $subject, $message, $headers); echo "<center><h2>Заявка отправлена, мы свяжемся с Вами в течении 15 минут.</h2>"; }?> 
  • And where is the form? - Kirill
  • And what does PHP have to do with it? - rjhdby
  • What exactly do you want? That the letter flies away only when something is selected, or the form could not be sent until some checkbox is checked? - Dmitry Zasypkin
  • If you do not use the if attribute, the empty form flies to the mail automatically, and if you list all the checkboxes and their 25, then it is not very convenient. So, how to say so - if one of the 25 checkboxes at least is pressed, the form is sent - Anton Rodionov

1 answer 1

For example, the following code:

 if (isset($_POST['ПонедельникУтро']) && $_POST['ПонедельникУтро'] == 'да') { echo("Выбран пункт про утро понедельника"); } 

Shows how you can verify that one of the checkboxes was selected. Accordingly, if you add the rest and combine them through OR ( || ) then you can achieve the desired.

But this approach is not very correct and leads to an increase in the entropy of the universe in the form of a Hindu code.

Therefore, it is better to alter the form. To do this, see how the form behaves if you specify checkboxes with the same name and different values.

For example, like this

 <td><input type="checkbox" name="time[]" value="ПонедельникУтро"></td> <td><input type="checkbox" name="time[]" value="ПонедельникДень"></td> 

And see what comes in PHP for different options to fill out the form.